Serialization
The process of saving or writing state of an object to a file is called
Serialization.
Serialization can be achieved by using FileOutputStream and
ObjectOutputStream.
Java supported object form to network/file supported form
De-Serialization
The process of reading state of an object from a file is called de-
serialization.
Can be achieved by using FileInputStream and ObjectInputStream
classes.
network/file supported form Java supported object form
Example
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
class Student implements Serializable
{
int CSIT = 33;
int AIML = 67;
}
public class SerializableDemo {
public static void main(String[] args) throws
IOException, ClassNotFoundException {
//Serialization
Student s1 = new Student();
System.out.println("Serialization process begins");
FileOutputStream fos = new FileOutputStream("stu.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(s1);
System.out.println("Serialization Completed");
//De-Serialization
FileInputStream fis = new FileInputStream("stu.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
Student s2 = (Student) ois.readObject();
System.out.println("De-Serialization Completed");
System.out.println(s2.CSIT);
System.out.println(s2.AIML);
}
}
Can perform Serialization for Serializable objects.
An object is said to be Serializable if and only if the corresponding class
implements Serializable interface.
Serializable interface available in java.io package
Serializable interface is a Marker interface i.e. it doesn’t contains any
methods(members).
The required ability will be provided by JVM.
Can add any number of objects to a file and read all those objects from a file.
Read the objects in the order of writing.
Trying to serialize a non-serializable object will raise
“NotSerializableException”.
The String class and all the wrapper classes implement the
java.io.Serializable interface by default.
Advantages of Serialization
To save/persist state of an object.
To travel an object across a network.
During default Serialization there may be a chance of lose
of information due to transient keyword.(Ex : mango ,money ,
box)
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
class Login implements Serializable
{
String name = "CVR";
String passwd = "csit";
}
public class SerializableDemo2 {
public static void main(String[] args) throws
IOException, ClassNotFoundException {
//Serialization
Login s1 = new Login();
System.out.println("Serialization process begins");
FileOutputStream fos = new
FileOutputStream("stu.ser");
ObjectOutputStream oos = new
ObjectOutputStream(fos);
oos.writeObject(s1);
System.out.println("Serialization Completed");
//De-Serialization
FileInputStream fis = new
FileInputStream("stu.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
Login s2 = (Login) ois.readObject();
System.out.println("De-Serialization Completed");
System.out.println(s2.name);
System.out.println(s2.passwd);
}
}
Output:
Serialization process begins
Serialization Completed
De-Serialization Completed
CVR
Null
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
class Login implements Serializable
{
String name = "CVR";
transient String passwd = "csit";
/* private void writeObject(ObjectOutputStream oos) throws
Exception
{
oos.defaultWriteObject();
String epwd = "369"+passwd;
oos.writeObject(epwd);
}
private void readObject(ObjectInputStream ois) throws
Exception
{
ois.defaultReadObject();
String epwd = (String)ois.readObject();
passwd = epwd.substring(3);
} */
}
public class SerializableDemo2 {
public static void main(String[] args) throws
IOException, ClassNotFoundException {
//Serialization
Login s1 = new Login();
System.out.println("Serialization process begins");
FileOutputStream fos = new
FileOutputStream("stu.ser");
ObjectOutputStream oos = new
ObjectOutputStream(fos);
oos.writeObject(s1);
System.out.println("Serialization Completed");
//De-Serialization
FileInputStream fis = new
FileInputStream("stu.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
Login s2 = (Login) ois.readObject();
System.out.println("De-Serialization Completed");
System.out.println(s2.name);
System.out.println(s2.passwd);
}
}
Serialization process begins
Serialization Completed
De-Serialization Completed
CVR
csit