-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathWriteObject.java
45 lines (33 loc) · 1.18 KB
/
WriteObject.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package lecture09.serial;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class WriteObject {
public static final String FILENAME = "src/lecture09/serial/students.dat";
public static void main(String[] args) {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(FILENAME);
ObjectOutputStream oos = new ObjectOutputStream(fos);
Student std1 = new Student(456, "Ahmed", 4.8f);
oos.writeObject(std1);
std1 = new Student(798, "Khalid", 5.0f);
oos.writeObject(std1);
std1 = new Student(132, "Saeed", 3.90f);
oos.writeObject(std1);
std1 = new Student(159, "Ali", 4.0f);
oos.writeObject(std1);
oos.close();
} catch (IOException e) {
System.err.printf("Unable to access (%s)\n", FILENAME);
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
System.err.println("Failed while closing the file!");
}
}
}
}
}