回退流

来源:互联网 发布:青少年近视调查数据 编辑:程序博客网 时间:2024/05/16 02:24

在 Java IO 中所有的数据都是采用顺序的读取方式,即对于一个输入流来说,都是采用从头到尾的顺序读取的。如果在输入流中某个不需要的内容被读取出来,则只能通过程序将这些不需要的内容处理掉。为了解决这样的读取问题,在java 中提供了一种回退输入流,可以把读取进来的某些数据重新退回到输入流的缓冲区中。

范例:

package haizhu.com.push;import java.io.ByteArrayInputStream;import java.io.PushbackInputStream;public class PushInputStream {public static void main(String[] args) throws Exception{String str = "www.java.cn";ByteArrayInputStream bais = new ByteArrayInputStream(str.getBytes());PushbackInputStream push = new PushbackInputStream(bais);System.out.println("读取出来的数据是:");int temp = 0;while((temp = push.read())!= -1){if(temp == '.'){push.unread(temp);temp = push.read();System.out.print("(退回的数据:"+(char)temp+")");}else{System.out.print((char)temp);}}}}


原创粉丝点击