import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
/**
* A program that serializes and deserializes an Employee array.
*/
public class SerializeEmployeeTester {
public static void main(String[] args) throws IOException,
ClassNotFoundException {
Employee[] staff = new Employee[2];
staff[0] = new Employee("Fred Flintstone", 50000);
staff[1] = new Employee("Barney Rubble", 60000);
staff[0].setBuddy(staff[1]);
staff[1].setBuddy(staff[0]);
ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(
"staff.dat"));
out.writeObject(staff);
out.close();
ObjectInputStream in = new ObjectInputStream(new FileInputStream(
"staff.dat"));
Employee[] staff2 = (Employee[]) in.readObject();
in.close();
for (Employee e : staff2)
System.out.println(e);
}
}
class Employee implements Serializable {
public Employee(String name, double salary) {
this.name = name;
this.salary = salary;
this.buddy = this;
}
public void setBuddy(Employee buddy) {
this.buddy = buddy;
}
public String toString() {
return getClass().getName() + "[name=" + name + ",salary=" + salary
+ ",buddy=" + buddy.name + "]";
}
private String name;
private double salary;
private Employee buddy;
}