-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathDataOutputStreamDemo.java
31 lines (28 loc) · 1.15 KB
/
DataOutputStreamDemo.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
//Java program to demonstrate DataOutputStream
// This program uses try-with-resources. It requires JDK 7 or later.
import java.io.*;
public class DataOutputStreamDemo {
public static void main(String args[]) throws IOException {
// writing the data using DataOutputStream
try (DataOutputStream dout = new DataOutputStream(new FileOutputStream("test.txt"))) {
dout.writeDouble(1.1);
dout.writeInt(55);
dout.writeBoolean(true);
dout.writeChar('4');
} catch (FileNotFoundException ex) {
System.out.println("Cannot Open the Output File");
return;
}
// reading the data back using DataInputStream
try (DataInputStream din = new DataInputStream(new FileInputStream("test.txt"))) {
double a = din.readDouble();
int b = din.readInt();
boolean c = din.readBoolean();
char d = din.readChar();
System.out.println("Values: " + a + " " + b + " " + c + " " + d);
} catch (FileNotFoundException e) {
System.out.println("Cannot Open the Input File");
return;
}
}
}