Serialization is the process of converting an object into a sequence of bits so that it can be persisted on a storage medium (such as a file, or a memory buffer) or transmitted across a network connection link.
When the resulting series of bits is reread according to the serialization format, it can be used to create a semantically identical clone of the original object. For many complex objects, such as those that make extensive use of references, this process is not straightforward.
This process of serializing an object is also called
deflating or marshalling an object.
The
opposite operation, extracting a data structure from
a series of bytes, is deserialization (which is also called inflating or
unmarshalling).
Interface:-
import java.io.Serializable;
public class DemoInterface implements Serializable {
int rno;
String
name;
Double
per;
DemoInterface(int r,String n,double p){
rno=r;
name=n;
per=p;
}
public
String toString(){
return "\nStudent Record: \n"
+ "\nRollno : "+rno+"\nName: "+name+"\nPercentage:
"+per;
}}
DemoSerial:-
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class DemoSerial {
public
static void main(String[] args) {
try{
DemoInterface di = new DemoInterface(101,"Richard
Smith",50.90);
System.out.println(di);
System.out.println("\nObject is Serializing.");
ObjectOutputStream
oos = new ObjectOutputStream(new
FileOutputStream("serializing"));
oos.writeObject(di);
System.out.println("\nObject is Serialized");
oos.flush();
oos.close();
}
catch(Exception e){
System.out.println("\nException in Serializing:"+e);
}
try{
DemoInterface di1;
System.out.println("\nObject is DeSerializing.");
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("serializing"));
di1=(DemoInterface)ois.readObject();
ois.close();
System.out.println("\nObjec is Deserialized");
System.out.println("\nDesirialized Output is: \n"+di1);
}
catch(Exception
ei){
System.out.println("\nException in DeSerializing:"+ei);
}
}
}
0 comments:
Post a Comment