Java FileReader Class

The FileReader class of the java.io package can be used to read data (in characters) from files.

It extends the InputSreamReader class.

FileReader extends the InputStreamReader and Reader classes
FileReader extends InputStreamReader

Before you learn about FileReader, make sure you know about the Java File.


Create a FileReader

In order to create a file reader, we must import the java.io.FileReader package first. Once we import the package, here is how we can create the file reader.

1. Using the name of the file

FileReader input = new FileReader(String name);

Here, we have created a file reader that will be linked to the file specified by the name.

2. Using an object of the file

FileReader input = new FileReader(File fileObj);

Here, we have created a file reader that will be linked to the file specified by the object of the file.

In the above example, the data in the file are stored using some default character encoding.

However, since Java 11 we can specify the type of character encoding (UTF-8 or UTF-16) in the file as well.

FileReader input = new FileReader(String file, Charset cs);

Here, we have used the Charset class to specify the character encoding of the file reader.


Methods of FileReader

The FileReader class provides implementations for different methods present in the Reader class.

read() Method

  • read() - reads a single character from the reader
  • read(char[] array) - reads the characters from the reader and stores in the specified array
  • read(char[] array, int start, int length) - reads the number of characters equal to length from the reader and stores in the specified array starting from the position start

For example, suppose we have a file named input.txt with the following content.

This is a line of text inside the file.

Let's try to read the file using FileReader.

import java.io.FileReader;

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

    // Creates an array of character
    char[] array = new char[100];

    try {
      // Creates a reader using the FileReader
      FileReader input = new FileReader("input.txt");

      // Reads characters
      input.read(array);
      System.out.println("Data in the file: ");
      System.out.println(array);

      // Closes the reader
      input.close();
    }

    catch(Exception e) {
      e.getStackTrace();
    }
  }
}

Output

Data in the file:
This is a line of text inside the file.

In the above example, we have created a file reader named input. The file reader is linked with the file input.txt.

FileInputStream input = new FileInputStream("input.txt");

To read data from the file, we have used the read() method.

Note: The file input.txt should be present in the current working directory.


getEncoding() Method

The getEncoding() method can be used to get the type of encoding that is used to store data in the file. For example,

import java.io.FileReader;
import java.nio.charset.Charset;

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

    try {
      // Creates a FileReader with default encoding
      FileReader input1 = new FileReader("input.txt");

      // Creates a FileReader specifying the encoding
      FileReader input2 = new FileReader("input.txt", Charset.forName("UTF8"));

      // Returns the character encoding of the file reader
      System.out.println("Character encoding of input1: " + input1.getEncoding());
      System.out.println("Character encoding of input2: " + input2.getEncoding());

      // Closes the reader
      input1.close();
      input2.close();
    }

    catch(Exception e) {
      e.getStackTrace();
    }
  }
}

Output

The character encoding of input1: Cp1252
The character encoding of input2: UTF8

In the above example, we have created 2 file reader named input1 and input2.

  • input1 does not specify the character encoding. Hence the getEncoding() method returns the default character encoding.
  • input2 specifies the character encoding, UTF8. Hence the getEncoding() method returns the specified character encoding.

Note: We have used the Charset.forName() method to specify the type of character encoding. To learn more, visit Java Charset (official Java documentation).


close() Method

To close the file reader, we can use the close() method. Once the close() method is called, we cannot use the reader to read the data.


Other Methods of FileReader

Method Description
ready() checks if the file reader is ready to be read
mark() mark the position in file reader up to which data has been read
reset() returns the control to the point in the reader where the mark was set

To learn more, visit Java FileReader (official Java documentation).

Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges