Made some changes in your code as below.
First implement Serializable interface in class Employee.java
public void serailize() {
Employee e = new Employee();
e.setName("Reyan Ali");
e.setAddress("Phokka Kuan, Ambehta Peer");
e.setSSN(11122333);
e.setNumber(101);
try {
ObjectOutputStream out = new ObjectOutputStream(openFileOutput(
"employee.ser", MODE_PRIVATE));
out.writeObject(e);
out.close();
System.out.printf("Serialized data is saved in /tmp/employee.ser");
} catch (IOException i) {
i.printStackTrace();
}
}
public void deSerailize() {
Employee e = null;
try {
ObjectInputStream in = new ObjectInputStream(
openFileInput("employee.ser"));
e = (Employee) in.readObject();
in.close();
} catch (IOException i) {
i.printStackTrace();
return;
} catch (ClassNotFoundException c) {
System.out.println("Serialized class not found");
c.printStackTrace();
return;
}
System.out.println("Deserialized Employee...");
System.out.println("Name: " + e.name);
System.out.println("Address: " + e.address);
System.out.println("SSN: " + e.SSN);
System.out.println("Number: " + e.number);
}
OpenFileInput() and openFileOutPut() are the application private files.
I think it is good to store serialized data in files.