/* * @topic T11332 Spring 2016 Serializable demo * @brief class Employee implements Serializable */ package demo; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.Serializable; public class Employee implements Serializable { public String name; public String address; public transient int SSN; public int ID; public void saveToFile(String fileName) { try { FileOutputStream fileOut = new FileOutputStream(fileName); ObjectOutputStream out = new ObjectOutputStream(fileOut); out.writeObject(this); out.close(); fileOut.close(); System.out.println("Serialized data is saved in " + fileName); } catch (IOException i) { i.printStackTrace(); } } public void loadFromFile(String fileName) { Employee employee; try { FileInputStream fileIn = new FileInputStream(fileName); ObjectInputStream in = new ObjectInputStream(fileIn); employee = (Employee) in.readObject(); in.close(); fileIn.close(); } catch (IOException i) { i.printStackTrace(); return; } catch (ClassNotFoundException c) { System.out.println("Employee class not found"); c.printStackTrace(); return; } this.ID = employee.ID; this.SSN = employee.SSN; this.address = employee.address; this.name = employee.name; } public void mailCheck() { System.out.println("Mailing a check to " + name + " " + address); } }//class Employee