in this post, i will summarize Serialization & DeSerialization in java, i am reading this chapter from Java Head First book and i found it very interesting “I am crazy about Head First Series”, i get the following screenshot including all the notes, and @end of my post i implement two methods as example to implement Serialization & DeSerialization in java code…..

import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
public class MySerializer
{
public void SerializeObject()
{
ObjectOutputStream objectOutputStream = null;
try
{
FileOutputStream fileOutputStream = new FileOutputStream(“MyFile”);
objectOutputStream = new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeInt(1);
objectOutputStream.writeInt(2);
objectOutputStream.writeInt(3);
}
catch (FileNotFoundException ex)
{
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
catch (IOException ex)
{
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
finally
{
try
{
objectOutputStream.close();
}
catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public void DeSerializeObject()
{
ObjectInputStream objectInputStream = null;
try
{
FileInputStream fileOutputStream = new FileInputStream(“MyFile”);
objectInputStream = new ObjectInputStream(fileOutputStream);
int x=objectInputStream.readInt();
objectInputStream.readInt();
objectInputStream.readInt();
System.out.println(x);
}
catch (FileNotFoundException ex)
{
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
catch (IOException ex)
{
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
finally
{
try
{
objectInputStream.close();
}
catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
My Reference: Java Head First Book