Can I peek on a BufferedReader?

There is nothing we can do picture There is nothing we can do · Mar 25, 2010 · Viewed 23.8k times · Source

Is there a way to check if in BufferedReader object is something to read? Something like C++ cin.peek(). Thanks.

Answer

Gavin H picture Gavin H · Mar 25, 2010

You can use a PushbackReader. Using that you can read a character, then unread it. This essentially allows you to push it back.

PushbackReader pr = new PushbackReader(reader);
char c = (char)pr.read();
// do something to look at c
pr.unread((int)c); //pushes the character back into the buffer