forked from JustinSDK/JavaSE6Tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPushbackStreamDemo.java
More file actions
45 lines (40 loc) · 1.48 KB
/
PushbackStreamDemo.java
File metadata and controls
45 lines (40 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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();
}
}
}