-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadWriteData.java
More file actions
40 lines (34 loc) · 1.42 KB
/
ReadWriteData.java
File metadata and controls
40 lines (34 loc) · 1.42 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
// demonstrate I/O binary operations on files
import java.io.*;
class ReadWriteData {
public static void main(String[] args) {
int i = 10;
double d = 1023.56;
boolean b = true;
// try to write some binary data
try (DataOutputStream aFileOut =
new DataOutputStream(new FileOutputStream("test_binary_data")))
{
System.out.printf("Writing %d to file.\n", i);
aFileOut.writeInt(i);
System.out.printf("Writing %.2f to file.\n", d);
aFileOut.writeDouble(d);
System.out.println("Writing " + b + " to file.");
aFileOut.writeBoolean(b);
} catch (IOException aExc) {
System.out.println("Cannot write file due to exception: " + aExc);
return;
}
// try to read some binary data
try (DataInputStream aFileIn =
new DataInputStream(new FileInputStream("test_binary_data")))
{
System.out.println("Reading i from file: " + aFileIn.readInt());
System.out.println("Reading d from file: " + aFileIn.readDouble());
System.out.println("Reading b from file: " + aFileIn.readBoolean());
} catch (IOException aExc) {
System.out.println("Cannot read file due to exception: " + aExc);
}
// TODO: figure out how DataInputStream recognizes binary data in sequence
}
}