Serialization and Deserialization in Java | Code Factory

package com.codeFactory;import java.io.Serializable;/**
* @author code.factory
*
*/
public class Employee implements Serializable {
private int id;
private String name;
public Employee(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "Employee : [id : " + id + ", name : " + name + "]";
}
}
package com.codeFactory;import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
/**
* @author code.factory
*
*/
public class SerializableUtility {
public void serialize(Object object, String filePath) {
try {
FileOutputStream fos = new FileOutputStream(filePath);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(object);
oos.flush();
oos.close();
} catch (Exception e) {
e.printStackTrace();
}
}

public Object deserialize(String filePath) {
Object object = null;
try {
FileInputStream fis = new FileInputStream(filePath);
ObjectInputStream ois = new ObjectInputStream(fis);
object = ois.readObject();
} catch (Exception e) {
e.printStackTrace();
}
return object;
}
}
package com.codeFactory;/**
* @author code.factory
*
*/
public class Main {
final private static String filePath = "Test.txt";

public static void main(String... args) {
Employee emp = new Employee(1, "Code Factory");

SerializableUtility su = new SerializableUtility();
su.serialize(emp, filePath);

Employee employee = (Employee) su.deserialize(filePath);
System.out.println(employee);
}
}
Employee : [id : 1, name : Code Factory]

Is sub class Serializable?

package com.codeFactory;/**
* @author code.factory
*
*/
public class Person extends Employee {
private String name;public Person(String name) {
super(2, name);
this.name = name;
}
public String getName() {
return name;
}
}
package com.codeFactory;/**
* @author code.factory
*
*/
public class Main {
final private static String filePath = "Test.txt";

public static void main(String... args) {
Employee emp = new Employee(1, "Code Factory");
Person person = new Person("Person");

SerializableUtility su = new SerializableUtility();
su.serialize(person, filePath);

Employee employee = (Employee) su.deserialize(filePath);
System.out.println(employee);
}
}
Employee : [id : 2, name : Person]

Other Class Reference in a Serializable class

package com.codeFactory;import java.io.Serializable;/**
* @author code.factory
*
*/
public class Employee implements Serializable {
private int id;
private Person person;
public Employee(int id, Person person) {
this.id = id;
this.person = person;
}
public int getId() {
return id;
}
public Person getPerson() {
return person;
}
@Override
public String toString() {
return "Employee : [id : " + id + ", person : " + person.getName() + "]";
}
}
package com.codeFactory;/**
* @author code.factory
*
*/
public class Person {
private String name;public Person(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
package com.codeFactory;/**
* @author code.factory
*
*/
public class Main {
final private static String filePath = "Test.txt";

public static void main(String... args) {
Employee emp = new Employee(1, new Person("Code Factory"));

SerializableUtility su = new SerializableUtility();
su.serialize(emp, filePath);

Employee employee = (Employee) su.deserialize(filePath);
System.out.println(employee);
}
}
java.io.NotSerializableException: com.codeFactory.Person
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source)
at java.io.ObjectOutputStream.writeSerialData(Unknown Source)
at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source)
at java.io.ObjectOutputStream.writeObject0(Unknown Source)
at java.io.ObjectOutputStream.writeObject(Unknown Source)
at com.codeFactory.SerializableUtility.serialize(SerializableUtility.java:18)
at com.codeFactory.Main.main(Main.java:15)

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store