📄 pushbackstreamdemo.java
字号:
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();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -