The PushbackReader class can read and return characters to the input stream.
Here are its two constructors:
PushbackReader(Reader inputStream)
PushbackReader(Reader inputStream, int bufSize)
PushbackReader has unread()
methods to return one or more characters to the invoking input stream.
It has the three forms shown here:
void unread(int ch) throws IOException void unread(char buffer [ ]) throws IOException void unread(char buffer [ ], int offset, int numChars) throws IOException
import java.io.CharArrayReader; import java.io.IOException; import java.io.PushbackReader; public class Main { public static void main(String args[]) { String s = "if (a == 4) a = 0;\n"; char buf[] = new char[s.length()]; s.getChars(0, s.length(), buf, 0); CharArrayReader in = new CharArrayReader(buf); int c;//from w ww . ja v a 2s. c om try (PushbackReader f = new PushbackReader(in)) { while ((c = f.read()) != -1) { switch (c) { case '=': if ((c = f.read()) == '=') System.out.print(".eq."); else { System.out.print("<-"); f.unread(c); } break; default: System.out.print((char) c); break; } } } catch (IOException e) { System.out.println("I/O Error: " + e); } } }