Tutorials About

Java IO: PushbackReader

Last updated: 2015-09-10

The Java PushbackReader class ( is intended to be used when you parse data from a Reader. Sometimes you need to read ahead a few characters to see what is coming, before you can determine how to interpret the current character. The PushbackReader allows you to do that. Well, actually it allows you to push back the read characters into the Reader. These characters will then be read again the next time you call read().

The Java PushbackReader works much like the PushbackInputStream except that the PushbackReader works on characters, whereas the PushbackInputStream works on bytes.

PushbackReader Example

Here is a simple PushbackReader example:

PushbackReader pushbackReader =
    new PushbackReader(new FileReader("c:\\data\\input.txt"));

int data =;


The call to read() reads a character from the PushbackReader just like from any other Reader. The call to unread() pushes a character back into the PushbackReader. The next time read() is called the pushed back characters will be read first. If you push back multiple characters into the PushbackReader, the latest pushed back character will be returned first from the read() method, just like with a stack.

Setting the Push Back Limit of a PushbackReader

You can set the number of characters you should be able to unread in the constructor of the PushbackReader. Here is how to set the pushback limit using the PushbackReader constructor:

int pushbackLimit = 8;
PushbackReader reader = new PushbackReader(
                                new FileReader("c:\\data\\input.txt"),

This example sets an internal buffer of 8 characters in the PushbackReader. That means you can unread at most 8 characters at a time, before reading them again.

Closing a PushbackReader

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

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


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

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

try(PushbackReader pushbackReader =
    new PushbackReader(reader)){

    int data =;
    while(data != -1) {
        System.out.print((char) data);
        data =;

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 PushbackReader is closed it will also close the Reader instance it reads from, so the FileReader instance will get closed when the PushbackReader 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.