概述
/*
缓冲的一个新颖的用法是实现推回(pushback)。Pushback用于输入流允许字节被读取然后返回(即“推回”)到流。PushbackInputStream类实现了这个想法。它提供了一种机制来“窥视”在没有受到破坏的情况下输入流生成了什么。
PushbackInputStream有两个构造函数:
PushbackInputStream(InputStream inputStream)
PushbackInputStream(InputStream inputStream, int numBytes)
第一种形式创建了一个允许一个字节推回到输入流的流对象。第二种形式创建了一个具有numBytes长度缓冲区的推回缓冲流。它允许多个字节推回到输入流。除了具有与InputStream相同的方法,PushbackInputStream提供了unread( )方法,表示如下:
void unread(int ch)
void unread(byte buffer[ ])
void unread(byte buffer, int offset, int numChars)
第一种形式推回ch的低位字节,它将是随后调用read( )方法所返回的下一个字节。第二种形式返回buffer缓冲器中的字节。第三种形式推回buffer中从offset处开始的numChars个字节。如果在推回缓冲器为满时试图返回一个字节,IOException异常将被引发。Java 2 对PushbackInputStream作了一些小的修改:它实现skip( )方法。
*/
// Demonstrate unread().
import java.io.*;
class PushbackInputStreamDemo{
public static void main(String[] args) throws IOException{
String s = "if (a == 4) a = 0;/n";
byte buf[] = s.getBytes();
ByteArrayInputStream in = new ByteArrayInputStream(buf);
PushbackInputStream f = new PushbackInputStream(in);
int c;
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;
}
}
}
}
最后
以上就是怕孤独面包为你收集整理的PushbackInputStream(推回输入流) 示例的全部内容,希望文章能够帮你解决PushbackInputStream(推回输入流) 示例所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复