For serializing object by implementing Externalizable interface, we need to override writeExternal() and readExternal() for serialization process to happen.
For Serialization process override writeExternal() method & for DeSerialization process by override readExternal() method.
Let’s customize Serialization process by overriding writeExternal() method :
public void writeExternal(ObjectOutput oo) throws IOException {
System.out.println("in writeExternal()");
oo.writeInt(id);
oo.writeObject(name);
}
|
We have serialized id and name manually by writing them in file.
Let’s customize DeSerialization process by overriding readExternal() method :
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
System.out.println("in readExternal()");
this.id=in.readInt();
this.name=(String)in.readObject();
}
|
We have DeSerialized id and name manually by reading them from file.
Full Program/SourceCode to Serialize Object by calling writeExternal() method of Externalizable interface>
package SerDeser3ImplementExternalizable;
import java.io.Externalizable;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
/*Author : AnkitMittal Copyright- contents must not be reproduced in any form*/
class Employee implements Externalizable {
private static final long serialVersionUID = 1L;
private Integer id;
private String name;
public Employee(){} //This constructor is called during deSerializaition process, as we have implemented Externalizable.
public Employee(Integer id, String name) {
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + "]";
}
/*
* define how Serialization process will write objects.
*/
@Override
public void writeExternal(ObjectOutput oo) throws IOException {
System.out.println("in writeExternal()");
oo.writeInt(id);
oo.writeObject(name);
}
/*
* define how deSerialization process will read objects.
*/
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
System.out.println("in readExternal()");
this.id=in.readInt();
this.name=(String)in.readObject();
}
}
public class SerializeEmployee {
public static void main(String[] args) {
Employee object1 = new Employee(1, "amy");
Employee object2 = new Employee(2, "ankit");
try {
OutputStream fout = new FileOutputStream("ser.txt");
ObjectOutput oout = new ObjectOutputStream(fout);
System.out.println("Serialization process has started, serializing employee objects...");
oout.writeObject(object1);
oout.writeObject(object2);
fout.close();
oout.close();
System.out.println("Object Serialization completed.");
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
/*OUTPUT
Serialization process has started, serializing employee objects...
in writeExternal()
in writeExternal()
Object Serialization completed.
*/
|
Full Program/SourceCode to DeSerialize object by calling readExternal() method of Externalizable interface>
package SerDeser3ImplementExternalizable;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
/*Author : AnkitMittal Copyright- contents must not be reproduced in any form*/
public class DeSerializeEmployee {
public static void main(String[] args){
try{
InputStream fin=new FileInputStream("ser.txt");
ObjectInput oin=new ObjectInputStream(fin);
System.out.println("DeSerialization process has started, displaying deSerialized employee objects...");
//while( oin.readObject()!=null );
Employee emp;
while( (emp=(Employee)oin.readObject())!=null ){
System.out.println(emp);
}
fin.close();
oin.close();
}catch(IOException | ClassNotFoundException e){
//e.printStackTrace();
}
System.out.println("Object DeSerialization completed.");
}
}
/*OUTPUT
DeSerialization process has started, displaying deSerialized employee objects...
in readExternal()
Employee [id=1, name=amy]
in readExternal()
Employee [id=2, name=ankit]
Object DeSerialization completed.
*/
|
RELATED LINKS>
Customizing Serialization process >
Serializing and DeSerializing Singleton >
Interviews >