← Back to Week 3 Hub

BufferedReader Pipeline

Reading a file line by line: the file on disk, the reader that opens it, the buffer that makes it efficient, and your readLine() loop.

FileReader

Opens the file and reads one character at a time. Works, but each character trip to the disk is slow.

BufferedReader

Wraps a FileReader. Pulls a block of characters into memory at once, then hands them to you line by line with readLine().

Your Loop

Keep calling readLine() until it returns null. That means end-of-file — stop reading and close().

Why the buffer? One big read from disk is much faster than many small ones. The BufferedReader fills its internal buffer in a single trip, then serves lines to your code from memory.

Pipeline

📄 poem.txt5 lines, ready
Disk
poem.txt
FileReader
char-by-char
BufferedReader
holds a buffer
Your code
readLine() loop
BufferedReader's buffer0 chars

Java code

Console output

$ java PoemReader
(click Run to start)
Status: idle

Tip: use Next step to walk through each readLine(), or press Auto-play to watch the loop run. Notice how readLine() returns null at end-of-file — that's what stops the loop.

← try/catch Flow FileWriter vs BufferedWriter →