Java FileReader Class

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
154946
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
154946
5
Start Learning
Topics Covered

In Java, we have a FileReader class, which belongs to the java.io package. Basically, we use this FileReader class to read the data from any given file. The filereader in Java reads the data in form of characters from the file. Hence, it is also known as a character-oriented class. In the form of bytes similar to the FileInputStream class, the data will be returned. The main purpose of this class is for file handling in Java.

In Java, we have a File class that belongs to the java.io package. Using the File class, we can perform numerous operations on files and directories.

The filereader in Java either uses the platform’s default charset or the specified charset for decoding from bytes to characters. Let us learn about them in detail.

Java FileReader Class Declaration

Now, Let us learn how to declare a filereader in Java.

Declaration:

Constructors of FileReader Class

There are several constructors we can use to create the filereader in Java; let us discuss them in detail.

ConstructorParametersDescription
FileReader(File fileObject)It consists of only one parameter of type FileWith this constructor, a new FileReader instance is created by reading the file data given in the parameter. Here, we give the file to read. This constructor uses the default charset.
FileReader(FileDescriptor files)It consists of only one parameter of type FileDescriptorWith this constructor, a new FileReader instance is created by reading the file description given in the parameter. This constructor uses the default charset.
FileReader(File fileObject, Charset charset)It consists of two parameters, of type Fileand CharsetWith this constructor, a new FileReader instance is created by reading the file data given in the parameter; here, we give the file to read. This constructor uses the specified charset given in the parameter.
FileReader(String fileName)It consists of only one parameter of type StringWith this constructor, a new FileReader instance is created, with the given FileName to read; here, we give the name of the file to read. This constructor uses the default charset.
FileReader(String fileName, Charset charset)It consists of two parameters, of type String, and CharsetWith this constructor, a new FileReader instance is created by reading the file data given in the parameter, here we give the name of the file to read. This constructor uses the specified charset given in the parameter.

Methods of FileReader Class

Let us discuss about all the methods of filereader in Java in detail.

IndexMethodDescription
1read()The read() method of the Java FileReader class reads the data of the file and returns a single character, and in case the stream is ended, it will return -1.
2read(char[] charBuffer, int offset, int length)The read(char[] charBuffer, int offset, int length) method of the Java FileReader class reads a stream of characters, and it stores those characters in a char array or Character Buffer given in the parameter. This method also consists of different parameters like offset, the position from which the FileReader will start reading, and the length, which is the total number of characters to be read. It returns plenty of characters to read, and if the stream ends, it will return -1.
3ready()The ready() method of the Java FileReader class returns true if the stream is ready to be read. A stream can be read if its input buffer is not empty or blank.
4getEncoding()The getEncoding() method of the Java FileReader class is used to return the character encoding's title, which is being used by the stream.
5close()The close() method of the Java FileReader class is used to close all the streams and liberate the system resources.

Java FileReader Example

In the example below, we read the FileReader.txt file using the FileReader class.

Output:

Conclusion

  • The filereader in Java belongs to the java.io package.
  • The Java FileReader class extends the *InputStreamReader` class.
  • The Java FileReader class uses either the platform’s default charset or the specified charset to decode from bytes to characters.
  • The Java FileReader class is a public class that inherits or extends the InputStreamReader class.
  • There are several constructors of FileReader class, like FileReader(File file), FileReader(FileDescriptor fd), FileReader(File file, Charset charset), FileReader(String filename), and FileReader(String filename, Charset charset).
  • There are different methods of FileReader class inJava, like read(), read(char[] charBuffer, int offset, int length), ready(), getEncoding(), close().