FileReader Class in Java

In this article, we will learn how to use FileReader class in Java with an example.

1. Introduction to FileReader

The FileReader class creates a Reader that you can use to read the contents of a file. FileReader is meant for reading streams of characters. For reading streams of raw bytes, consider using a FileInputStream.

2. FileReader Constructors

  • FileReader(File file) - Creates a new FileReader, given the File to read from.
  • FileReader(FileDescriptor fd) - Creates a new FileReader, given the FileDescriptor to read from.
  • FileReader(String fileName) - Creates a new FileReader, given the name of the file to read from.

3. FileReader Class Methods

  • int read() - It is used to return a character in ASCII form. It returns -1 at the end of a file.
  • void close() - It is used to close the FileReader class.

4. FileReader Class Example

Let's create FileReaderExample class that showcases reading and displaying content from a file named sample.txt.
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class FileReaderExample {

    public static void main(String[] args) {
        // Assuming a file named sample.txt exists in the current directory

        // 2. Constructing a FileReader
        try (FileReader fileReader1 = new FileReader("sample.txt")) {

            System.out.println("Reading from sample.txt:");

            // 3. Reading from a File

            // Reading character by character
            int singleChar;
            while ((singleChar = fileReader1.read()) != -1) {
                System.out.print((char) singleChar);
            }

        } catch (IOException e) {
            System.out.println("Error reading the file: " + e.getMessage());
        }
    }
}

// Expected Output (based on the content of sample.txt):
// Reading from sample.txt:
// [Contents of sample.txt displayed here]
Before executing the FileReaderExample class, ensure you have a file named sample.txt in the current working directory. The program will then read and display its content. If the file doesn't exist or there's another IO error, an error message will be printed.

Always ensure to close the FileReader after use. This can be achieved using the close() method or the try-with-resources statement.

5. Advantages of FileReader 

Character Encoding: It takes care of character encoding automatically, saving developers from manual conversion. 

Readability: It provides a high-level API that's easier to use compared to InputStream for reading character data. 

6. When to Use FileReader?

Text Data Reading: Use FileReader specifically when dealing with character-based (text) files. It automatically decodes bytes to characters using the default character encoding of the system. This makes it suitable for reading textual content from files. 

Simpler File Reading: When you need a straightforward mechanism to read character data from a file without much additional functionality or configuration, FileReader serves as a convenient choice. It provides a direct link between files and character-oriented reading operations. 

Basis for Buffered Reading: FileReader can be wrapped with BufferedReader to efficiently read large text files line by line. The combination allows for enhanced performance and additional utility methods, like reading entire lines at once.

7. Limitations and Considerations 

Not Suitable for Binary Data: It's designed for character data. For binary data, prefer using FileInputStream

Dependent on System Encoding: Unless you take precautions, it uses the default system encoding, which can cause issues if files have a different encoding. 

Comments