Course contentsShow
← Java
Lesson 1978 of 2,87040. File I/O: Character StreamsFree lesson

Creating BufferedReader: Wrapping a Reader

Learn to construct a BufferedReader by wrapping FileReader or other Reader instances for efficient character input.

Creating BufferedReader: Wrapping a Reader

What you'll learn: How to construct a BufferedReader by wrapping FileReader or other Reader instances to dramatically improve character reading performance.

The Core Pattern: Wrapping for Efficiency

You've seen that buffering matters for character I/O performance. BufferedReader doesn't read files directly—instead, it wraps around an existing Reader (like FileReader) to add buffering capabilities. Think of it like putting a thermos around a coffee cup: the cup still holds the coffee, but the thermos makes accessing it more efficient.

The constructor pattern is straightforward:

Reader reader = new FileReader("data.txt");
BufferedReader buffered = new BufferedReader(reader);

Or more commonly, in a single line:

BufferedReader buffered = new BufferedReader(new FileReader("data.txt"));

Why Wrapping?

This design follows the Decorator Pattern. BufferedReader doesn't need to know whether it's wrapping a FileReader, StringReader, or any other Reader type—it just adds buffering to whatever character source you give it. This makes the design flexible and reusable.

The Buffer Size Option

Like BufferedInputStream, you can specify a custom buffer size:

BufferedReader buffered = new BufferedReader(
    new FileReader("large-file.txt"),
    16384  // 16KB buffer
);

The default buffer size is 8KB, which works well for most scenarios.

Resource Management

When you close a BufferedReader, it automatically closes the wrapped Reader:

try (BufferedReader br = new BufferedReader(new FileReader("data.txt"))) {
    // Reading operations
} // Both BufferedReader and FileReader close automatically

You don't need separate try-with-resources for both streams—closing the outermost wrapper handles everything.

Key Takeaway: BufferedReader wraps any Reader instance to add internal buffering, dramatically reducing system calls and improving read performance without changing how you open the underlying file or character source.