-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadWriteData.java
More file actions
47 lines (44 loc) · 1.44 KB
/
Copy pathReadWriteData.java
File metadata and controls
47 lines (44 loc) · 1.44 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
// Write and then read back binary data.
import java.io.*;
public class ReadWriteData {
public static void main(String[] args)
{
int i = 10;
double d = 1023.56;
boolean b = true;
// Write some values.
try (DataOutputStream dataOut =
new DataOutputStream(new FileOutputStream("testdata")))
{
System.out.println("Writing " + i);
dataOut.writeInt(i);
System.out.println("Writing " + d);
dataOut.writeDouble(d);
System.out.println("Writing " + b);
dataOut.writeBoolean(b);
System.out.println("Writing " + 12.2 * 7.4);
dataOut.writeDouble(12.2 * 7.4);
}
catch(IOException exc) {
System.out.println("Write error.");
return;
}
System.out.println();
// Now, read them back.
try (DataInputStream dataIn =
new DataInputStream(new FileInputStream("testdata")))
{
i = dataIn.readInt();
System.out.println("Reading " + i);
d = dataIn.readDouble();
System.out.println("Reading " + d);
b = dataIn.readBoolean();
System.out.println("Reading " + b);
d = dataIn.readDouble();
System.out.println("Reading " + d);
}
catch(IOException exc) {
System.out.println("Read error.");
}
}
}