CodeBlanks Logo Icon CodeBlanks

BufferedReader

Learn to read lines with BufferedReader.

Reading lines with BufferedReader

BufferedReader wraps FileReader for more efficient reading. Its main method is readLine(): it returns one line as a String, without the line terminator. At end of file it returns null.

Usage: BufferedReader r = new BufferedReader(new FileReader("a.txt"));. Loop with String line = r.readLine(); while (line != null) { ... line = r.readLine(); }. BufferedWriter speeds up writing; newLine() adds a platform-independent line separator.

Using BufferedReader

You create a BufferedReader by wrapping a Reader, not directly: new BufferedReader(new FileReader(path)). This is the decorator pattern: BufferedReader extends FileReader's capabilities. read() still reads a single character, but readLine() gives the whole line at once.

For large files, the buffer reduces disk access; reading is much faster. The default buffer size is 8 KB.

Practice Read a file line by line with BufferedReader

Import java.io.BufferedReader and java.io.FileReader. Add throws IOException to main. First write a file: FileWriter fw = new FileWriter("line.txt"); fw.write("One\nTwo"); fw.close();. Then open with BufferedReader r = new BufferedReader(new FileReader("line.txt"));, declare String line;, loop with while ((line = r.readLine()) != null) printing each line. Close with r.close().

Starter code

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

public class Main {
    public static void main(String[] args) throws IOException {
        // First write two lines to a file

        // Open with BufferedReader, read line by line, print

    }
}

Sample solution

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

public class Main {
    public static void main(String[] args) throws IOException {
        FileWriter fw = new FileWriter("line.txt");
        fw.write("One\nTwo");
        fw.close();
        BufferedReader r = new BufferedReader(new FileReader("line.txt"));
        String line;
        while ((line = r.readLine()) != null) {
            System.out.println(line);
        }
        r.close();
    }
}

Expected output

One
Two

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What does BufferedReader.readLine() return at end of file?

  • An empty string ("")
  • null Correct answer
  • -1
  • A line terminator character

Explanation: readLine() returns null at end of file; the loop ends on null.

2 Complete the line-reading loop

Question
String line;
while ((line = r.____()) != null) {
    System.out.println(line);
}
Answer
String line;
while ((line = r.readLine()) != null) {
    System.out.println(line);
}

Explanation: readLine returns a line; null ends the loop.

3 Match the BufferedReader concepts

Term Meaning
readLine() Returns one line
null End of file
FileReader Class wrapped by BufferedReader
decorator The wrapping pattern

All lessons in Intermediate