DEV Community

Cover image for FileReader in Java
Ezhil Abinaya K
Ezhil Abinaya K

Posted on

FileReader in Java

FileReader
The FileReader class in Java is used to read data from a file in the form of characters. It is a character-oriented stream that makes it ideal for reading text files.
There are several ways to read files.

  • Scanner - best for simple text and when you want to parse numbers or words easily.
  • BufferedReader - best for large text files, because it is faster and reads line by line.
  • FileInputStream - best for binary data (images, audio, PDFs) or when you need full control of raw bytes.

Constructors of Java FileReader Class
1. FileReader(String fileName)
Creates a new FileReader object to read data from a file with the given name.

FileReader fr = new FileReader("example.txt");
Enter fullscreen mode Exit fullscreen mode

2. FileReader(File file)
Creates a new FileReader object to read from the specified File object.

File file = new File("example.txt");
FileReader fr = new FileReader(file);
Enter fullscreen mode Exit fullscreen mode

Hierarchy of the FileReader class.


References
https://www.geeksforgeeks.org/java/java-io-filereader-class/
https://www.w3schools.com/JAVA/java_files_read.asp

Top comments (0)