forked from davidjava1991/java17CompleteCourse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample1.java
More file actions
35 lines (33 loc) · 1.04 KB
/
Copy pathExample1.java
File metadata and controls
35 lines (33 loc) · 1.04 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
package com.david.class135;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class Example1 {
public static void main(String[] args) {
Employee e1 = new Employee(1,"David","Accounting");
try {
FileOutputStream fo = new FileOutputStream("./out/Employee.txt");
ObjectOutputStream os = new ObjectOutputStream(fo);
os.writeObject(e1);
os.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
FileInputStream fi = new FileInputStream("./out/Employee.txt");
ObjectInputStream oi = new ObjectInputStream(fi);
Employee e2 = (Employee)oi.readObject();
System.out.println("id= "+e2.id+", name= "+e2.name+", department = "+e2.department);
oi.close();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}