Tutorials About

Java IO: BufferedReader

Last updated: 2015-09-09

The Java BufferedReader class ( provides buffering to your Reader instances. Buffering can speed up IO quite a bit. Rather than read one character at a time from the network or disk, the BufferedReader reads a larger block at a time. This is typically much faster, especially for disk access and larger data amounts.

The Java BufferedReader is similar to the BufferedInputStream but they are not exactly the same. The main difference between BufferedReader and BufferedInputStream is that BufferedReader reads characters (text), whereas the BufferedInputStream reads raw bytes.

BufferedReader Example

To add buffering to a Reader instance, simply wrap it in a BufferedReader. Here is how that looks:

BufferedReader bufferedReader = new BufferedReader(
                      new FileReader("c:\\data\\input-file.txt"));

This example creates a BufferedReader which wraps a FileReader. The BufferedReader will read a block of characters from the FileReader (typically into a char array). Each character returned from read() is thus returned from this internal array. When the array is fully read the BufferedReader reads a new block of data into the array etc.

BufferedReader Buffer Size

You can set the buffer size to use internally by the BufferedReader. You provide the size as a constructor parameter, like this:

int bufferSize = 8 * 1024;

BufferedReader bufferedReader = new BufferedReader(
                      new FileReader("c:\\data\\input-file.txt"),

This example sets the internal buffer to 8 KB. It is best to use buffer sizes that are multiples of 1024 bytes. That works best with most built-in buffering in hard disks etc.

Except for adding buffering to Reader instances, a Java BufferedReader behaves pretty much like a Reader. The BufferedReader has one extra method though, the readLine() method. This method can be handy if you need to read input one line at a time. Here is a BufferedReader readLine() example:

String line = bufferedReader.readLine();

The readLine() method will return a textual line (all text until at line break is found) read from the BufferedReader. If there is no more data to read from the underlying Reader, then the BufferedReader's readLine() method will return null .

Closing a BufferedReader

When you are finished reading characters from the BufferedReader you should remember to close it. Closing a BufferedReader will also close the Reader instance from which the BufferedReader is reading.

Closing a BufferedReader is done by calling its close() method. Here is how closing a BufferedReader looks:


You can also use the try-with-resources construct introduced in Java 7. Here is how to use and close a BufferedReader looks with the try-with-resources construct:

Reader reader = new FileReader("data/data.bin");

try(BufferedReader bufferedReader =
    new BufferedReader(reader)){

    String line = bufferedReader.readLine();
    while(line != null) {
        //do something with line

        line = bufferedReader.readLine();


Notice how there is no longer any explicit close() method call. The try-with-resources construct takes care of that.

Notice also that the first FileReader instance is not created inside the try-with-resources block. That means that the try-with-resources block will not automatically close this FileReader instance. However, when the BufferedReader is closed it will also close the Reader instance it reads from, so the FileReader instance will get closed when the BufferedReader is closed.

Connect with me: Newsletter - Get all my free tips!

Close TOC
This website uses cookies to improve the user experience and gather statistics. Our advertisers use cookies too (3rd party cookies), to provide more relevant ads. Continued use of this website implies that you accept the use of cookies on this website. We do not share our cookies with our advertisers, and our advertisers do not share cookies with us.