package onlyfun.caterpillar;
import java.io.*;
public class PushbackStreamDemo {
public static void main(String[] args) {
try {
PushbackInputStream pushbackInputStream =
new PushbackInputStream(
new FileInputStream(args[0]));
byte[] array = new byte[2];
int tmp = 0;
int count = 0;
while((count = pushbackInputStream.read(array))
!= -1) {
// 两个字节转换为整数
tmp = (short)((array[0] << 8) |
(array[1] & 0xff)); // 没看懂
tmp = tmp & 0xFFFF; // 没看懂
// 判断是否为BIG5,如果是则显示BIG5中文字
if(tmp >= 0xA440 && tmp < 0xFFFF) {
System.out.println("BIG5: " +
new String(array));
}
else {
// 将第二个字节推回流
pushbackInputStream.unread(array, 1, 1);
// 显示ASCII范围的字符
System.out.println("ASCII: " +
(char)array[0]);
}
}
pushbackInputStream.close();
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println("请指定文件名称");
}
catch(IOException e) {
e.printStackTrace();
}
}
}