Open In App

Java FileReader Class

Last Updated : 29 Aug, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

FileReader in Java is a class in the java.io package which can be used to read a stream of characters from the files. Java IO FileReader class uses either specified charset or the platform's default charset for decoding from bytes to characters.

  1. Charset: The Charset class is used to define methods for producing encoders and decoders and for recovering several names combined with the charset.
  2. Default Charset: The default charset is defined during implicit computer start-up and it depends on the locale and charset of the underlying operating system.

The following image shows the Hierarchical Flow of FileReader class.

Hierarchical Flow of FileReader Class
Hierarchical Flow of FileReader Class

Constructors of Java FileReader Class

The Constructors inside FileReader are shown in the table below.

Constructor

Description

FileReader(File file)Creates a new FileReader with the the given File to read (using default charset)
FileReader(FileDescriptor fd)Creates a new FileReader with given FileDescriptor to read (using default charset)
FileReader(File file, Charset charset)Creates a new FileReader with a given File to read (using the given charset)
FileReader(String filename)Creates a new FileReader with a a given FileName to read (using default charset)
FileReader(String filename, Charset charset)Creates a new FileReader with given File to read (using the given charset)

Methods of Java FileReader Class

The methods under FileReader are shown in the table below.

MethodDescription
read()Reads a single character and returns it, or -1 if the end of the stream is reached.
read(char[] charBuffer, int offset, int length)Reads characters into the given buffer starting at offset up to length characters. Returns the number of characters read or -1 if the stream ends.
ready()Checks if the stream is ready to be read (i.e., input buffer is not empty).
getEncoding()Returns the name of the character encoding used by the stream.
close()Closes the stream and releases system resources.

Example:

Java
import java.io.*;

class GFG {
    public static void main(String[] args)
    {
        try {
            // FileReader Class used
            FileReader fileReader = new FileReader("gfg.txt");

            System.out.println("Reading char by char : \n");
            int i;

            // Using read method
            while ((i = fileReader.read()) != -1) {
                System.out.print((char)i);
            }

            System.out.println("Reading using array : \n");
            char[] charArray = new char[10];

            // Using read method for to get character array
            fileReader.read(charArray);
            System.out.print(charArray);

            // Close method called
            fileReader.close();
            System.out.println("FileReader closed!");
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}

 Output

Reading char by char :
GeeksForGeeks
Reading using array :
GeeksForGeeks
FileReader closed!