-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJava172_stream.java
More file actions
65 lines (57 loc) · 1.43 KB
/
Copy pathJava172_stream.java
File metadata and controls
65 lines (57 loc) · 1.43 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package java0911_stream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Java172_stream {
public static void main(String[] args) {
DataOutputStream ds = null;
FileOutputStream fs = null;
DataInputStream di = null;
FileInputStream is = null;
try {
fs = new FileOutputStream("src/java0911_stream/sample.txt");
ds = new DataOutputStream(fs);
ds.writeInt(65); // 'A'
ds.write(65);
ds.writeDouble(10.5);
ds.writeChar('a');
ds.writeUTF("java");
ds.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
ds.close();
fs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("=======================");
try {
is = new FileInputStream("src/java0911_stream/sample.txt");
di = new DataInputStream(is);
System.out.println(di.readInt());
System.out.println(di.read());
System.out.println(di.readDouble());
System.out.println(di.readChar());
System.out.println(di.readUTF());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
di.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}