0
0
JavaHow-ToBeginner · 4 min read

How to Use FileReader in Java: Simple Guide with Example

In Java, FileReader is used to read character files by creating an instance with the file path and then reading characters or lines from it. You typically wrap FileReader with a BufferedReader for efficient reading line by line.
📐

Syntax

The basic syntax to use FileReader involves creating a FileReader object with the file path, then reading characters or lines from it. It is common to wrap it with BufferedReader to read lines easily.

  • FileReader fr = new FileReader(String filePath); - opens the file for reading characters.
  • BufferedReader br = new BufferedReader(fr); - wraps FileReader to read lines efficiently.
  • String line = br.readLine(); - reads one line of text.
  • fr.close(); or br.close(); - closes the file to free resources.
java
FileReader fr = new FileReader("filename.txt");
BufferedReader br = new BufferedReader(fr);
String line = br.readLine();
br.close();
💻

Example

This example shows how to read a text file line by line using FileReader and BufferedReader. It prints each line to the console.

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

public class FileReaderExample {
    public static void main(String[] args) {
        try (FileReader fr = new FileReader("example.txt");
             BufferedReader br = new BufferedReader(fr)) {
            String line;
            while ((line = br.readLine()) != null) {
                System.out.println(line);
            }
        } catch (IOException e) {
            System.out.println("Error reading file: " + e.getMessage());
        }
    }
}
Output
Hello, this is line 1. This is line 2. End of file.
⚠️

Common Pitfalls

Common mistakes when using FileReader include:

  • Not closing the reader, which can cause resource leaks.
  • Using FileReader for binary files (it only reads characters).
  • Not handling IOException properly.
  • Assuming readLine() reads the whole file at once (it reads one line at a time).

Always use try-with-resources or close readers in a finally block.

java
/* Wrong way: Not closing reader */
FileReader fr = new FileReader("file.txt");
BufferedReader br = new BufferedReader(fr);
String line = br.readLine();
// No close() called - resource leak

/* Right way: Using try-with-resources */
try (FileReader fr2 = new FileReader("file.txt");
     BufferedReader br2 = new BufferedReader(fr2)) {
    String line2 = br2.readLine();
} catch (IOException e) {
    e.printStackTrace();
}
📊

Quick Reference

Summary tips for using FileReader:

  • Use FileReader to read text files character by character.
  • Wrap with BufferedReader to read lines easily.
  • Always close readers to avoid resource leaks.
  • Handle IOException with try-catch.
  • Use try-with-resources for automatic closing.

Key Takeaways

Use FileReader with BufferedReader to read text files line by line efficiently.
Always close your FileReader or BufferedReader to free system resources.
Handle IOException to avoid program crashes when file operations fail.
Try-with-resources is the best way to manage FileReader lifecycle automatically.
FileReader reads characters, so do not use it for binary files.