-
Notifications
You must be signed in to change notification settings - Fork 20
/
Demo.java
60 lines (43 loc) · 1.56 KB
/
Demo.java
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package lecture05.usingmark;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class Demo {
public static void main(String[] args) throws IOException {
File file = new File("src/lecture05/usingmark/data.txt");
FileInputStream fis = new FileInputStream(file);
BufferedInputStream bis = new BufferedInputStream(fis);
if (fis.markSupported()) {
System.out.println("FileInputStream support mark");
} else {
System.out.println("FileInputStream does not support mark");
}
if (bis.markSupported()) {
System.out.println("BufferedInputStream support mark");
} else {
System.out.println("BufferedInputStream does not support mark");
}
bis.mark(10);
byte[] buff = new byte[300];
for (int i = 0; i < 10; i++) {
bis.read(buff);
System.out.printf("%d: %s\n", i, byte2string(buff, 10));
}
bis.reset();
System.out.println("\n------------------------------------\n");
bis.read(buff);
System.out.println( byte2string(buff, 10));
fis.close();
}
private static String byte2string(byte[] buff) {
return byte2string(buff, buff.length);
}
private static String byte2string(byte[] buff, int len) {
String str = "";
for (int i = 0; i < buff.length && i < len; i++) {
str += (char) buff[i];
}
return str;
}
}