I'm learning about Serializable and Externalizable interface and I see that, when an Externalizable object is reconstructed, an instance is created first using the public no-arg constructor, then the readExternal method is called. If the object does not support Externalizable, then Serializable objects are restored by reading them from an ObjectInputStream.
I don't understand why we use ObjectInputStream for Externalization if the object isn't readed from there? What exactly is readed from the ObjectInputStream? I think we read something from there if we use it.
Also I found this chart about Deserialization Externalizable or Serializable interface
What is the difference between Serializable and Externalizable at the deserialization process?
I don't understand why the Externalizable objects aren't restored by reading them from an ObjectInputStream in the same way like Serializable objects?
ObjectInputStream ois = new ObjectInputStream(
new FileInputStream("employee.ser"))
I know that the FileInputStream opens a file, creates a sequence of bytes based on the data in the file. The ObjectInputStream takes a sequence of bytes, recreats the object based on the sequence of bytes.
And here is a code.
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
public class Employee implements Externalizable {
private int id;
private String name;
private int age;
public void Employee() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", age=" + age + "]";
}
public void writeExternal(ObjectOutput oo) throws IOException {
System.out.println("Inside writeExternal method");
oo.writeInt(id);
oo.writeObject(name);
}
public void readExternal(ObjectInput oi) throws IOException, ClassNotFoundException {
System.out.println("Inside readExternal method");
id = oi.readInt();
name = (String) oi.readObject();
}
}
ExternalizableWrite
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class ExternalizableWrite {
public static void main(String args[]) {
ExternalizableWrite ew = new ExternalizableWrite();
ew.writeEmployeeObject();
}
private void writeEmployeeObject() {
try (ObjectOutputStream oos = new ObjectOutputStream(
new FileOutputStream("employee.ser"))) {
Employee employee = new Employee();
employee.setId(101);
employee.setName("Peter");
employee.setAge(25);
System.out.println(employee);
oos.writeObject(employee); // write the specified object to the ObjectOutputStream
System.out.println("Successfully written employee object to the file.\n");
} catch (FileNotFoundException ex) {
System.out.printf("ERROR: %s", ex);
} catch (IOException ex) {
System.out.printf("ERROR: %s", ex);
}
}
}
ExternalizableRead
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
public class ExternalizableRead {
public static void main(String args[]) {
ExternalizableRead er = new ExternalizableRead();
er.readEmployeeObject();
}
private void readEmployeeObject() {
try (ObjectInputStream ois = new ObjectInputStream(
new FileInputStream("employee.ser"))) {
Employee employee = (Employee) ois.readObject();
System.out.println(employee);
System.out.println("Successfully read employee objecct to the file.\n");
} catch (FileNotFoundException ex) {
System.out.printf("ERROR: %s", ex);
} catch (IOException | ClassNotFoundException ex) {
System.out.printf("ERROR: %s", ex);
}
}
}