Java, .NET, PHP, PYTHON, ANGULAR, ML, Data Science, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

Serialization and Deserialization Concept in Java:-
.................................................................................................................................

Serialization means convert object data to Stream(file) and deserialization means Convert Stream(file)  to Object.

Stream means the Sequence of bytes.

for example, if we store student data into student objects and we want to write Student object data to file then we will use data serialization.


If we have Student data in the file and we want to co-relate file data to object then we can use de-serialization.



Java provides a Serializable Interface to implement Serialization this interface does not contain any method it only contains behavior hence It is also called Marker Interface.



Java Provide ObjectOuputStream Class and ObjectInputStream Class to Write and Read Data into Object.




 
Complete Example of Serialization and De-serialization?

import java.io.*;
class Student implements Serializable
{
   int rno;
   String sname;
   Student(int rno,String sname)
   {
      this.rno=rno; 
      this.sname=sname;

   } 

}
class SerExample
{
     public static void main(String args[])  throws

IOException,ClassNotFoundException
     {
         File f = new File("mno.txt");
         /*Student obj = new Student(1001,"xyz");
        
         FileOutputStream fo = new FileOutputStream(f);
         ObjectOutputStream oo = new ObjectOutputStream(fo);
         oo.writeObject(obj);  // object to stream
         fo.close(); */
         Student obj1=null;
         FileInputStream fi = new FileInputStream(f);
         ObjectInputStream oi = new ObjectInputStream(fi);
         obj1 = (Student)oi.readObject();   // stream to object
         System.out.print(obj1.rno+" ,"+obj1.sname);
         
       

     }





Note:-  What is a transient keyword in Java?

It is used to protect the variable from serialization for example if we do not want to serialize a particular data member of the class then we can use transient keyword


Now we are taking three variable rno and name is the instance variable and the fee is a transient variable that can not be serialized.

import java.io.Serializable;

/**
 *
 * @author Hp
 */
public class Student implements Serializable {
    int rno;
    String sname;
    transient int fees;
    Student(int rno,String sname,int fees)
    {
        this.rno = rno;
        this.sname=sname;
        this.fees = fees;
    }
    
    
}


Write the code for data serialization and deserialization:-


import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 *
 * @author Hp
 */
public class SerializationExample {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Student obj = new Student(1001,"xyz",45090);
        FileOutputStream fo = new FileOutputStream("D://studentser.txt");
        ObjectOutputStream oo = new ObjectOutputStream(fo);
        oo.writeObject(obj);
        oo.close();
        
        Student obj1 = null;
        FileInputStream fi = new FileInputStream("D://studentser.txt");
        ObjectInputStream oi = new ObjectInputStream(fi);
        obj1 =(Student) oi.readObject();
        System.out.print("rno is "+obj1.rno + " name is  "+obj1.sname + "Fees is "+obj1.fees);
        fi.close();
        
    }
    
}





1 Comments

If you have any doubt in programming or join online classes then you can contact us by comment .

  1. Another example of Serialization and Deserialization?

    import java.io.*;
    class SI implements Serializable
    {
    float p,r,t,si;
    SI(float p,float r, float t)
    {
    this.p=p;
    this.r=r;
    this.t=t;
    }
    }

    class SIMain
    {
    public static void main(String args[]) throws Exception
    {
    /* FileOutputStream fo = new FileOutputStream("si.txt");
    SI obj = new SI(45000,2,2);
    obj.si = (obj.p*obj.r*obj.t)/100;
    ObjectOutputStream oo = new ObjectOutputStream(fo);
    oo.writeObject(obj);
    oo.close(); */
    SI s=null;
    FileInputStream fo = new FileInputStream("si.txt");
    ObjectInputStream oo = new ObjectInputStream(fo);
    s =(SI)oo.readObject();
    System.out.println(s.p + " "+s.r + " "+s.t + " "+s.si);
    oo.close();

    }

    }

    ReplyDelete

Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post