importjava.io.ByteArrayInputStream;//导入ByteArrayInputStream的包importjava.io.IOException;//导入IO异常包importjava.io.PushbackInputStream;//导入回退流包/***回退流操作**/...
import java.io.ByteArrayInputStream; //导入ByteArrayInputStream的包
import java.io.IOException; //导入IO异常包
import java.io.PushbackInputStream; //导入回退流包
/**
* 回退流操作
* */
public class PushBackInputStreamDemo{ //类名
public static void main(String[] args) throws IOException{ //入口点 抛出IO异常
String str = "hello,rollenholt"; //定义字符串
PushbackInputStream push = null; //声明回退流对象
ByteArrayInputStream bat = null; //声明字节数组流对象
bat = new ByteArrayInputStream(str.getBytes()); //创建字节数组流对象,将字符串str拆分成字节
push = new PushbackInputStream(bat); //创建回退流对象,将拆解的字节数组流传入
int temp = 0; //临时变量,存放逐字节读取
//从下面开始,最好每条语句都来个注释,
while((temp = push.read()) != -1){ //push.read()逐字节读取存放在temp中,如果读取完成返回-1
if(temp == ','){ 判断读取的是否是逗号
push.unread(temp); //是的话把逗号抛回输入流缓冲区
temp = push.read(); // 回退到缓冲区前面
System.out.print("(回退" + (char) temp + ") "); //输出回退的字符
}else{
System.out.print((char) temp); //否则输出字符
}
}
}
}
push.unread(temp); //是的话把逗号抛回输入流缓冲区
temp = push.read(); // 回退到缓冲区前面
这两句话不太理解
展开