Java FileReader Class read() Method with Examples

The FileReader class in Java is a subclass of InputStreamReader and is primarily used for reading character files.

Java FileReader Class read() Method:

This class in Java is used to read data from files in character streams. It inherits FileReader class. It is used for reading streams of characters. It is an implementation of InputStream for reading character files. This class creates a Reader that you can use to read the contents of a file.

‘read()’ Method:

The read() method in FileReader is used to read a single character from the file. It returns the character as an integer value. The integer value returned represents the next character of data, or -1 if the end of the file is reached.

  1. Initializing :
    • Initially, a FileReader object is created to facilitate reading from the specified file.
  2. Looping through Characters:
    • Subsequently, within a while loop, the program iterates through each character in the file.
  3. Reading Characters:
    • Utilizing the read() method, the FileReader reads each character individually.
    • Actively, it retrieves the next character from the file and returns its integer representation.
  4. Outputting Characters:
    • As characters are read, they are actively converted to their corresponding characters and printed to the console.
    • Thus, each character is sequentially displayed on the console until the end of the file is encountered.
  5. Handling Exceptions:
    • In case of an I/O error, indicated by an IOException , the program actively catches and handles the exception to prevent termination.
    • Meanwhile, appropriate error-handling procedures are executed to ensure robustness and reliability.
  6. Closing FileReader:
    • Finally, after all characters have been processed, the FileReader object is actively closed to release system resources.
    • Consequently, this ensures proper resource management and prevents resource leaks.

Syntax:

public int read() throws IOException
  • Return Value: The next character of data, or -1 if the end of the file is reached.
  • Throws: IOException – If an I/O error occurs.

Example:

import java.io.FileReader;
import java.io.IOException;

public class Main {
    public static void main(String[] args) {
        FileReader fileReader = null;
        try {
            fileReader = new FileReader("file.txt");
            int character;
            while ((character = fileReader.read()) != -1) {
                System.out.print((char) character);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (fileReader != null) {
                    fileReader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

The Java code reads characters from a file named “file.txt” (you should replace this with the path to your actual file), and prints each character to the console until the end of the file is reached. The output will be whatever is written in the file “file.txt”.

let’s consider an example where the “file.txt” contains the following text:

Hello, world!

This is a test file.

Output:

Hello, world! 
This is a test file.

Each character, including spaces, punctuation marks, and newline characters, will be printed exactly as they appear in the file.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top