Guide to BufferedReader – 缓冲读卡器指南

最后修改: 2018年 11月 7日

1. Overview


BufferedReader is a class which simplifies reading text from a character input stream. It buffers the characters in order to enable efficient reading of text data.


In this tutorial, we’re going to look at how to use the BufferedReader class.


2. When to Use BufferedReader


In general, BufferedReader comes in handy if we want to read text from any kind of input source whether that be files, sockets, or something else.


Simply put, it enables us to minimize the number of I/O operations by reading chunks of characters and storing them in an internal buffer. While the buffer has data, the reader will read from it instead of directly from the underlying stream.


2.1. Buffering Another Reader


Like most of the Java I/O classes, BufferedReader implements Decorator pattern, meaning it expects a Reader in its constructor. In this way, it enables us to flexibly extend an instance of a Reader implementation with buffering functionality:

像大多数Java I/O类一样,BufferedReader 实现了Decorator模式,意味着它在构造函数中期望有一个Reader通过这种方式,它使我们能够灵活地扩展具有缓冲功能的Reader实现的实例。

BufferedReader reader = 
  new BufferedReader(new FileReader("src/main/resources/input.txt"));

But, if buffering doesn’t matter to us we could just use a FileReader directly:


FileReader reader = 
  new FileReader("src/main/resources/input.txt");

In addition to buffering, BufferedReader also provides some nice helper functions for reading files line-by-line. So, even though it may appear simpler to use FileReader directly, BufferedReader can be a big help.

除了缓冲之外,BufferedReader 还提供了一些很好的辅助函数来逐行读取文件。因此,尽管直接使用FileReader 可能看起来更简单,但BufferedReader 可以提供很大帮助。

2.2. Buffering a Stream


In general, we can configure BufferedReader to take any kind of input stream as an underlying source. We can do it using InputStreamReader and wrapping it in the constructor:


BufferedReader reader = 
  new BufferedReader(new InputStreamReader(;

In the above example, we are reading from which typically corresponds to the input from the keyboard. Similarly, we could pass an input stream for reading from a socket, file or any imaginable type of textual input. The only prerequisite is that there is a suitable InputStream implementation for it.


2.3. BufferedReader vs Scanner


As an alternative, we could use the Scanner class to achieve the same functionality as with BufferedReader.


However, there are significant differences between these two classes which can make them either more or less convenient for us, depending on our use case:


  • BufferedReader is synchronized (thread-safe) while Scanner is not
  • Scanner can parse primitive types and strings using regular expressions
  • BufferedReader allows for changing the size of the buffer while Scanner has a fixed buffer size
  • BufferedReader has a larger default buffer size
  • Scanner hides IOException, while BufferedReader forces us to handle it
  • BufferedReader is usually faster than Scanner because it only reads the data without parsing it

With these in mind, if we are parsing individual tokens in a file, then Scanner will feel a bit more natural than BufferedReader. But, just reading a line at a time is where BufferedReader shines.


If needed, we also have a guide on Scanner as well.


3. Reading Text With BufferedReader


Let’s go through the entire process of building, using and destroying a BufferReader properly to read from a text file.


3.1. Initializing a BufferedReader


Firstly, let’s create a BufferedReader using its BufferedReader(Reader) constructor:


BufferedReader reader = 
  new BufferedReader(new FileReader("src/main/resources/input.txt"));

Wrapping the FileReader like this is a nice way to add buffering as an aspect to other readers.


By default, this will use a buffer of 8 KB. However, if we want to buffer smaller or larger blocks, we can use the BufferedReader(Reader, int) constructor:

默认情况下,这将使用一个8KB的缓冲区。然而,如果我们想缓冲更小或更大的块,我们可以使用BufferedReader(Reader, int)构造函数。

BufferedReader reader = 
  new BufferedReader(new FileReader("src/main/resources/input.txt")), 16384);

This will set the buffer size to 16384 bytes (16 KB).


The optimal buffer size depends on factors like the type of the input stream and the hardware on which the code is running. For this reason, to achieve the ideal buffer size, we have to find it ourselves by experimenting.


It’s best to use powers of 2 as buffer size since most hardware devices have a power of 2 as the block size.


Finally, there is one more handy way to create a BufferedReader using the Files helper class from the java.nio API:


BufferedReader reader = 

Creating it like this is a nice way to buffer if we want to read a file because we don’t have to manually create a FileReader first and then wrap it.


3.2. Reading Line-by-Line


Next, let’s read the content of the file using the readLine method:


public String readAllLines(BufferedReader reader) throws IOException {
    StringBuilder content = new StringBuilder();
    String line;
    while ((line = reader.readLine()) != null) {

    return content.toString();

We can do the same thing as above using the lines method introduced in Java 8 a bit more simply:

我们可以使用Java 8中引入的lines 方法,更简单地完成上述同样的事情

public String readAllLinesWithStream(BufferedReader reader) {
    return reader.lines()

3.3. Closing the Stream


After using the BufferedReader, we have to call its close() method to release any system resources associated with it. This is done automatically if we use a try-with-resources block:


try (BufferedReader reader = 
       new BufferedReader(new FileReader("src/main/resources/input.txt"))) {
    return readAllLines(reader);

4. Other Useful Methods


Now let’s focus on various useful methods available in BufferedReader.


4.1. Reading a Single Character


We can use the read() method to read a single character. Let’s read the whole content character-by-character until the end of the stream:

我们可以使用read() 方法来读取单个字符。让我们逐个读取整个内容的字符,直到流的末端。

public String readAllCharsOneByOne(BufferedReader reader) throws IOException {
    StringBuilder content = new StringBuilder();
    int value;
    while ((value = != -1) {
        content.append((char) value);
    return content.toString();

This will read the characters (returned as ASCII values), cast them to char and append them to the result. We repeat this until the end of the stream, which is indicated by the response value -1  from the read() method.

这将读取字符(以ASCII值的形式返回),将其转换为char 并将其追加到结果中。我们重复这样做,直到流的结束,即通过read()方法的响应值-1来表示。

4.2. Reading Multiple Characters


If we want to read multiple characters at once, we can use the method read(char[] cbuf, int off, int len):

如果我们想一次读取多个字符,我们可以使用read(char[] cbuf, int off, int len)方法。

public String readMultipleChars(BufferedReader reader) throws IOException {
    int length;
    char[] chars = new char[length];
    int charsRead =, 0, length);

    String result;
    if (charsRead != -1) {
        result = new String(chars, 0, charsRead);
    } else {
        result = "";

    return result;

In the above code example, we’ll read up to 5 characters into a char array and construct a string from it. In the case that no characters were read in our read attempt (i.e. we’ve reached the end of the stream), we’ll simply return an empty string.


4.3. Skipping Characters


We can also skip a given number of characters by calling the skip(long n) method:

我们还可以通过调用skip(long n)方法来跳过指定的字符数。

public void givenBufferedReader_whensSkipChars_thenOk() throws IOException {
    StringBuilder result = new StringBuilder();

    try (BufferedReader reader = 
           new BufferedReader(new StringReader("1__2__3__4__5"))) {
        int value;
        while ((value = != -1) {
            result.append((char) value);

    assertEquals("12345", result);

In the above example, we read from an input string which contains numbers separated by two underscores. In order to construct a string containing only the numbers, we are skipping the underscores by calling the skip method.


4.4. mark and reset


We can use the mark(int readAheadLimit) and reset() methods to mark some position in the stream and return to it later. As a somewhat contrived example, let’s use mark() and reset() to ignore all whitespaces at the beginning of a stream:

我们可以使用mark(int readAheadLimit)reset()方法来标记流中的某个位置,并在以后返回。作为一个有点矫揉造作的例子,让我们使用mark()reset()来忽略一个流开始时的所有空白处。

public void givenBufferedReader_whenSkipsWhitespacesAtBeginning_thenOk() 
  throws IOException {
    String result;

    try (BufferedReader reader = 
           new BufferedReader(new StringReader("    Lorem ipsum dolor sit amet."))) {
        do {
        } while(Character.isWhitespace(

        result = reader.readLine();

    assertEquals("Lorem ipsum dolor sit amet.", result);

In the above example, we use the mark() method to mark the position we just read. Giving it a value of 1 means only the code will remember the mark for one character forward. It’s handy here because, once we see our first non-whitespace character, we can go back and re-read that character without needing to reprocess the whole stream. Without having a mark, we’d lose the L in our final string.


Note that because mark() can throw an UnsupportedOperationException, it’s pretty common to associate markSupported() with code that invokes mark(). Though, we don’t actually need it here. That’s because markSupported() always returns true for BufferedReader.

请注意,由于mark()可以抛出UnsupportedOperationException,所以将markSupported()与调用mark()的代码联系起来是相当普遍的。尽管如此,我们在这里实际上并不需要它。这是因为markSupported() 总是为BufferedReader返回true。

Of course, we might be able to do the above a bit more elegantly in other ways, and indeed mark and reset aren’t very typical methods. They certainly come in handy, though, when there is a need to look ahead.


5. Conclusion


In this quick tutorial, we’ve learned how to read character input streams on a practical example using BufferedReader.


Finally, the source code for the examples is available over on Github.