-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDataStreamsDemo.java
More file actions
50 lines (45 loc) · 1.2 KB
/
Copy pathDataStreamsDemo.java
File metadata and controls
50 lines (45 loc) · 1.2 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
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public class DataStreamsDemo{
private static final String FILE_PATH = "DataStreams.dat";
public static void main(String[] args){
FileOutputStream fos = null;
FileInputStream fis = null;
DataOutputStream dos = null;
DataInputStream dis = null;
try{
fos = new FileOutputStream(FILE_PATH);
dos = new DataOutputStream(fos);
dos.writeInt(2016);
dos.writeUTF("Saving this string in UTF-8 encoding format");
dos.writeFloat(1.25F);
dos.close(); // close underlying output stream
dos = null; // avoid another will-failure closing attempt.
fis = new FileInputStream(FILE_PATH);
dis = new DataInputStream(fis);
System.out.println(dis.readInt());
System.out.println(dis.readUTF());
System.out.println(dis.readFloat());
}catch(IOException ioe){
ioe.printStackTrace();
}finally{
if(dos!=null){
try{
dos.close();
}catch(IOException ioe){
ioe.printStackTrace();
}
}
if(dis!=null){
try{
dis.close();
}catch(IOException ioe){
ioe.printStackTrace();
}
}
}
}
}