Jenkov.com
Tutorials About




Java IO: BufferedReader


Last updated: 2014-06-23

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

The main difference between BufferedReader and BufferedInputStream is that Reader's work on characters (text), wheres InputStream's works on raw bytes.

To add buffering to your Reader's simply wrap them in a BufferedReader. Here is how that looks:

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

Simple, isn't it? You can set the buffer size to use internally by the BufferedReader. You provide the size as a constructor parameter, like this:

    Reader input = new BufferedReader(
                          new FileReader("c:\\data\\input-file.txt"),
                      8 * 1024
    );

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 your Reader's, BufferedReader behaves pretty much like a Reader. It has one extra method though, the readLine() method. This method can be handy if you need to read input one line at a time.



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.
OK