Serialization and Deserialization Concept in Java

0


Serialization means to convert Object data to stream and Deserialization means to Convert Stream Data to Object.

Serialization means the conversion of data from Object pattern to file pattern .it is mostly used to transfer data from one device to another or one network to another for security concern because serializable data is not readable after deserialization it will be readable form.

For example, if Student Object Contain Student Information then we want to write object data directly in the file then we can use Data Serialization process.

Java Provide Serializable Interface to perform data serialization. It is also called a marker interface because this interface does not contain any method definition.

class Student implements Serializable
{



}




Transient:-  it is a modifier in Java which is used to protect data member from serialization.

transient datatype variable name;

no need to write transient under static variable.




Example of Serialization and Deserialization:-

public class Student implements Serializable{
     public int rno;
    public String sname;
    public transient int fee;
    Student(int rno,String sname,int fee)
    {
        this.rno=rno;
        this.sname=sname;
        this.fee=fee;
    }
 
}

..........................................................
import java.io.*;

public class SerializationExample {
    public static void main(String[] args) throws IOException,ClassNotFoundException {
        File f = new File("E://demo1.txt");
        /*Student s = new Student(1001,"manish");
        FileOutputStream fo = new FileOutputStream(f);
        ObjectOutputStream oo = new ObjectOutputStream(fo);
        oo.writeObject(s);
        oo.close();
        fo.close();*/
        Student s = null;
        FileInputStream fi = new FileInputStream(f);
        ObjectInputStream oi = new ObjectInputStream(fi);
         s = (Student)oi.readObject();
         System.out.println(s.rno+" "+s.sname);
     
     
    }
 
}







Post a Comment

0Comments

POST Answer of Questions and ASK to Doubt

Post a Comment (0)