-
Notifications
You must be signed in to change notification settings - Fork 79
Expand file tree
/
Copy pathObjectStreamTest.java
More file actions
45 lines (36 loc) · 1.24 KB
/
Copy pathObjectStreamTest.java
File metadata and controls
45 lines (36 loc) · 1.24 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
package objectStream;
import java.io.*;
/**
* @version 1.10 17 Aug 1998
* @author Cay Horstmann
*/
class ObjectStreamTest
{
public static void main(String[] args) throws IOException, ClassNotFoundException
{
Employee harry = new Employee("Harry Hacker", 50000, 1989, 10, 1);
Manager carl = new Manager("Carl Cracker", 80000, 1987, 12, 15);
carl.setSecretary(harry);
Manager tony = new Manager("Tony Tester", 40000, 1990, 3, 15);
tony.setSecretary(harry);
Employee[] staff = new Employee[3];
staff[0] = carl;
staff[1] = harry;
staff[2] = tony;
// save all employee records to the file employee.dat
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("employee.dat")))
{
out.writeObject(staff);
}
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream("employee.dat")))
{
// retrieve all records into a new array
Employee[] newStaff = (Employee[]) in.readObject();
// raise secretary's salary
newStaff[1].raiseSalary(10);
// print the newly read employee records
for (Employee e : newStaff)
System.out.println(e);
}
}
}