Using PushBackInputStream in Java

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.

Share

No related posts.

Leave a Reply

  

  

  

You can use these HTML tags

<a href=""title=""><abbr title=""><acronym title=""><b><blockquote cite=""><cite><code><del datetime=""><em><i><q cite=""><strike><strong>

A sample text widget

Etiam pulvinar consectetur dolor sed malesuada. Ut convallis euismod dolor nec pretium. Nunc ut tristique massa.

Nam sodales mi vitae dolor ullamcorper et vulputate enim accumsan. Morbi orci magna,tincidunt vitae molestie nec,molestie at mi. Nulla nulla lorem,suscipit in posuere in,interdum non magna.