Write an Remote Method Invocation Program for Object Serialization.

Aim: - Write an RMI Program for serialization.
Tools: - NetBeans IDE, jdk

Theory:-

Ø  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).

 

Advantages Of Object Serialization:

 

Ø  A method of persisting objects which is more convenient than writing their properties to a text file on disk, and re-assembling them by reading this back in.

Ø  A method of issuing remote procedure calls, e.g., as in SOAP

Ø  Amethod for distributing objects, especially in software componentry such as COM, CORBA, etc.

Ø  A method for detecting changes in time-varying data.

 

Program:-

 

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);
        }             
    }

}

Output:-


Post a Comment

0 Comments