The PushbackInputStream class provides a pushback buffer so a program can “unread”bytes. In other words,it can add bytes to the stream and then read them. This class allows to add data to the stream while they’re reading it. The next time data is read from the stream,the unread bytes are reread.
Example:
import java.io.IOException;import java.io.PushbackInputStream;public class Main{ public static void main(String[] args) throws IOException{ PushbackInputStream in = new PushbackInputStream(System.in,3); in.unread(0); in.unread(5); in.unread(10); System.out.println(in.read()); System.out.println(in.read()); System.out.println(in.read()); }}Note:common use for PushbackInputStream is tokenizing source code.
Download code from this post.
No related posts.

Recent Comments