- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadWriteData.java
More file actions
Latest commit
40 lines (34 loc) · 1.42 KB
/
Copy pathReadWriteData.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
importjava.io.*;
classReadWriteData {
publicstaticvoidmain(String[] args) {
inti = 10;
doubled = 1023.56;
booleanb = true;
// try to write some binary data
try (DataOutputStreamaFileOut =
newDataOutputStream(newFileOutputStream("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 (IOExceptionaExc) {
System.out.println("Cannot write file due to exception: " + aExc);
return;
}
// try to read some binary data
try (DataInputStreamaFileIn =
newDataInputStream(newFileInputStream("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 (IOExceptionaExc) {
System.out.println("Cannot read file due to exception: " + aExc);
}
// TODO: figure out how DataInputStream recognizes binary data in sequence
}
}