Java FileReader

The FileReader class in Java is used to read character or text data from the file. It is defined in the java.io package and it is a subclass of InputStreamReader class. The InputStreamReader is the subclass of the Reader class. Both the InputStreamReader and Reader classes are abstract classes. All these classes were introduced in Java 1.1 version.

Constructors

Since FileReader is a connection based, where one end is a Java application and another end is a File. We must always pass the file information to the constructor. Therefore, it doesn’t contain 0 parameter constructor.

It has total 5 constructors,

  • public FileReader(String fileName) throws FileNotFoundException
  • public FileReader(File file) throws FileNotFoundException
  • public FileReader(FileDescriptor fd)

The other two constructors were introduced in Java 11 version are,

  • public FileReader(String fileName, Charset charset) throws IOException
  • public FileReader(File file, Charset charset) throws IOException

In all these constructors, we get FileNotFoundException if the named file does not exist, or it is a directory rather than a regular file, or for some other reason file cannot be opened for reading.

Methods

FileReader class doesn’t contain any own methods. All methods of super class and interfaces are inherited to the FileReader. Some important methods are,

Return typeMethodDescription
intread()It reads a single character at a time.
intread(char[] ch)It reads characters, stores them to the passed char array, and returns the number of characters it read.
booleanready()It tells whether the stream is ready to be read.
voidclose()It closes the stream and releases any system resources associated with it.

Java FileReader Example

The read() method reads a single character from the file and returns its Unicode value. If the next character is not available i.e. at end of the file then it returns -1. Since read() method returns Unicode value represented as int value, so at the time of printing, we must use typecasting.

We have a file “data.txt” in the current working directory. The text in the file “data.txt” is,

Hello World!
Number = 20
Welcome to Java.

// Java program to read the data.txt
import java.io.FileReader;
import java.io.IOException;
public class FileReaderDemo {

   public static void main(String[] args) 
                  throws IOException {

      // create FileReader object
      FileReader fr = new FileReader("data.txt");

      // define a int variable to 
      // store returned value and call read()
      int i = fr.read();
      while(i != -1) {

         // convert int to char and display
         System.out.print((char)i);
         i = fr.read();
      }

      // close the stream
      fr.close();
   }
}

Output:-
Hello World!
Number = 20
Welcome to Java.

It logic to read the data also can be written as,

int i ;
while( (i= fr.read()) != -1) {
   System.out.print((char)i);
}

Instead for checking data = -1 better to use ready() method. The ready() method tells whether the stream is ready to be read or not. When it encounters the end of the file then it returns false.

while(fr.ready()) {
   // convert int to char and display
   System.out.print((char)fr.read());
}

Example using read(char[] ch)

Another alternate method read(char[] ch) is also given, which reads enough character from the file, store into the char array, and returns the number of characters copied from the file.

Assume file contains a total of 100 characters,
char[] ch = new char[20];
System.out.println( fr.read(ch) ); // 20
// use loop to display array data

char[] ch = new char[5000];
System.out.println( fr.read(ch) ); // 100
// use loop to display array data

If we fix the size of the char array then we may not get the complete data from the file or wastage of the memory. If the file contains 100 characters, and we declare char array size as 20 then it will read only 20 characters from the file. Similarly, when we declare char array size as 5000 and the file contains only 100 characters, so it reads only 100 characters and remaining memory wastage.

To solve this problem first find the length of characters available in the file, and we can find it using the length() method of java.io.File class. But length() method returns a long value and while declaring the char array size we must declare size within the int range only, so use type-casting.

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class FileReaderDemo {
   public static void main(String[] args) 
                  throws IOException {

      // create file class object
      File file = new File("data.txt");

      // find length
      long fileLength = file.length();

      // create FileReader class object
      FileReader fr = new FileReader(file);

      // declare char arry
      char[] ch = new char[(int)fileLength];

      // read character from file and store
      // into the char array
      int total = fr.read(ch);

      // display char array
      for(char c: ch) {
         System.out.print(c);
      }

      System.out.println("Length of the file = " 
      	                           + fileLength );
      System.out.println("Total character read = " 
      	                                + total );
      fr.close();
   }
}

Output:-
Hello World!
Number = 20
Welcome to Java.
Length of the file = 42
Total character read = 42

This technique will work only when number of characters available in the file doesn’t exceed the int range, else use read(int ch) method.

Limitation of FileReader class

1) Using FileReader class in Java we can’t read one line at a time from the file. We can read only one character at a time. It reduces the reading performance of the application. It gives very low performance when characters in the file are very large and file size exceeds the range of int data type.

2) FileReader can be used to read data only from the file not from the console because it doesn’t contain any constructor which can connect to another stream.

To solve this problem and to read one line at a time we must use BufferedReader class, it is the most convincing way to read character/text data either from the file or console.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *