Serialization is the process of writing an class object that is transmitted through the network in the encoded stream of byte form.
Serializable Exception
Serialization is the process of writing an class object that is transmitted through the network in the encoded stream of byte form. This Encoded form can be reconstructed from the network by reading from byte of stream. This Process is called Deserialization.Serialization allow to save the object in file and transmitting the object through network. It does not write the class variable, because these variable are not a part of object state. It allow the object to be serialized ,without writing the method of object.
Use Of Serialization
1)Serialization is used in various Java Technologies such as Enterprise java bean directly used with activate and passivate server.
2)The Technologies supports the invoking the method of an object on another host such as RMI AND CORBA used to transmit their parameter across the network
Interface Serializable
The Serializable class derived from java .io.serializable that defines no message. class implements serialization or extending class that also implements serialization define that class as one of the participants in serialization. The instance is used as parameter of ObjectOutputStream.writeObject and the result of ObjectInputStream.readObject.
When Exception or not exception is Thrown in Serialization
An object is encountered that is not serialized like your collection element, these
method throws not serialized exception. Incase, if an object contains variables
of class type that refer to objects whose classes are not serializable, the
object stream methods will NotSerializableException
when
attempting to write or read an instance. Even ,when a collection is
serializable but contains objects that are not serializable, an exception will
be show.
If that field refers to an instance of a serializable class, no exception occurs
upon serialization. The library classes such as serializable that includes
wrapper
, collection classes, GUI component classes, Date
,time, Color
, Point
, and URL
. There
are some library classes that are not serializable include Thread
,
reflection classes (Method
, etc.), stream classes, Socket
, Graphics
, and Image
. If that field refers to an
instance of a serializable class, no exception occurs upon serialization.
How to implement Serialization
The Serializable interface in java are java.io package that implements the object serialization in java.
1) The Serializable interface-This interface is used for serializing objects in java and does not have any method defined inside it.If a class is serializable all of its subclasses are also serialized.
Syntax used to implement serializable interface
class xyz(class_name) implements serializable
{
<class definition>
}
2) The ObjectOutputStream interface- This class extends the Output Stream class and implements its interface. This class writes an object of the primitive data type and data object to the Output stream.
List of the method in ObjectOutputStream:
Methods | Description |
Public void writeObject(Object obj)throws IO Exception | When you Write an obj Object to the ObjectOutputstream stream |
public void flush( ) throws IO Exception | This method allows the data available in a buffer is written to a file to which stream is connected. |
3)The ObjectInputStream interface-This class extends the Input Stream class and its interface of Object Input .The ObjectInputStream deserializes the data and object written using the ObjectOutputStream.
List of method in ObjectInputStream :
Methods | Description |
public final Object read Object( )throws IO Exception,ClassNotFoundException | This method is used to reads an object from ObjectInputStream |
public string readLine( )throws IO Exception | This method is used to read a line that has been terminated by End of File |
Understand Java Exception in Serialization Example
In this code we try to describe a class name Student that implements Serializable interface used to serialize its objects. We declare a constructor inside the class that accept the parameter and void ( ) method is used to display the parameter passed in the constructor. But on Runtime the command prompt show no method such found exception.
import java.io.*; public class Student implements Serializable { private String stud_name; private int stud_rollno; private String stud_address; public Student ( ) { |
Output On Command Prompt
C:\saurabh>javac Student.java C:\saurabh>java Student Exception in thread "main" java.lang.NoSuchMethodError: main |
How to Overcome Serialization Exception
In order to overcome exception, you need to serialize an object of the Student class using write Object( ) method of the ObjectOutputStream class.
WriteObject( ) |
This method write the object to the Output Object Stream i.e student.txt. |
flush( ) |
This ensure the data stored in buffer is written to a file to which stream is connected. |
close( ) |
This is used to release the resources occupied by stream. |
public class WriteStudent { public static void main(String args[]) { Student s=new Student("Saurabh",1,"New Delhi"); try { FileOutputStream fos=new FileOutputStream("student.txt"); ObjectOutputStream oos=new ObjectOutputStream(fos); oos.writeObject(s); oos.flush( ); oos.close( ); } catch(IOException ioe) { System.out.println("Exception: "+ioe.toString()); } } } |
In the above code an object of the student class is serialized into a file name student.txt.The above command prompt does not display any output on the screen, rather it create a file student.txt.
Output on Command Prompt
C:\Documents and Settings\Administrator>cd\ C:\>cd saurabh\ C:\saurabh>javac WriteStudent.java C:\saurabh>java WriteStudent |
The readobject method of the ObjectInputStream class is used to read the serialized object from the student.txt.
readObject( ) -is used to read the serialized object from the student.txt.
The object returned by the readobject( ) is typecast into required class.
s=(Student)ois.readObject( )
public class ReadStudent { public static void main(String args[]) { Student s=new Student(); try { FileInputStream fis=new FileInputStream("Student.txt"); ObjectInputStream ois=new ObjectInputStream(fis); s=(Student)ois.readObject( ); s.show( ); } catch(IOException ioe) { System.out.println("Excxeption:"+ioe.toString( ) ); } catch(ClassNotFoundException cnfe) { System.out.println("Exception:"+cnfe.toString( ) ); } } } |
Output on the Command prompt 0
|