Advertisement



< Prev
Next >



Java - Serialization





Process of Serialization and Deserialization.







Methods used in Serialization and Deserialization


The process of "serialization" and "deserialization" is performed by using the methods of two high level stream classes, ObjectOutputStream and ObjectInputStream. These methods are -:


Methods Description
writeObject()
This method of ObjectOutputStream class writes the state of an object to the stream, so that it can be saved within a file, and thus performing serialization.

readObject()
This method of ObjectInputStream class reads the saved object from a file in which the object's state was saved, and thus performing deserialization.




Advertisement




Note :







Program with Serialization and Deserialization :


//Java - Example of Serialization and Deserialization 


import java.io.*;


//A class implementing Serializable interface 
class Serialz implements Serializable 	
{
int i=0;
public static void main(String... ar)
{
	Serialz ob= new Serialz();
	ob.i=10;	//object instance variable's value set to 10
	System.out.println("Before Serialization object has value : " + ob.i);
	try
	{
		FileOutputStream fos= new FileOutputStream("E:\\File.ser");//Saved state of the object is written to this file
		ObjectOutputStream oos= new ObjectOutputStream(fos);
		oos.writeObject(ob);	//Serializing, i.e. saving the state of this object, ob
		oos.close();
	}
	catch(Exception exp)
	{
		System.out.println(exp);
	}
	try	
	{
		FileInputStream fis= new FileInputStream("E:\\File.ser"); //Reading the same file for Deserialization
		ObjectInputStream ois= new ObjectInputStream(fis);
		ob = (Serialz)ois.readObject();	//Deserializing, i.e. restoring the object to its original state.
		ois.close();
		System.out.println("After Deserialization restored object has value : "+ ob.i);
	}
	catch(Exception exp	)
	{
	System.out.println(exp);
	}
}	//main method ends
}	//class definition ends



Output is -:


Before Serialization object has value : 10
After Deserialization restored object has value : 10


Program Analysis


In this code above, we are first performing the Serialization by calling the writeObject() method of ObjectOutputStream class, which saves the state of an object and its instance variable's value of int 10 in a file at path E:\\File.ser.

Mext, this serialized object is then deserialized by calling the readObject() method of ObjectInputStream class, which reads the serialized saved object, using which the original object is reconstucted with its instance variable having the same value 10, which it had at the time of Serialization.



Note :


The readObject() method returns the deserialized object, which is contained within an object of the Object class(the parent class of all Java classes). Hence, the casting of this deserialized object from Object type to its original type is required to reconstruct the original object.


Please share this article -





< Prev
Next >
< Console
Java transient keyword>



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement