Java FileReader class

Introduction

The FileReader class creates a Reader to read the contents of a file.

Two commonly used constructors are shown here:

FileReader(String filePath )  
FileReader(File fileObj ) 

The following example shows how to read lines from a file.

import java.io.FileReader;
import java.io.IOException;

public class Main {
   public static void main(String args[]) {

      try (FileReader fr = new FileReader("FileReaderDemo.java")) {
         int c;//from  ww w . ja  v a 2s  . c o  m

         // Read and display the file.
         while ((c = fr.read()) != -1)
            System.out.print((char) c);

      } catch (IOException e) {
         System.out.println("I/O Error: " + e);
      }
   }
}



PreviousNext

Related