Sunday, 2 February 2014

serialization

1.Serialization ?

          -- process of writing the state of an object into a byte stream which is known as serialization.
          --   State of an object” means data present in the object at any instance of time.
          -- If any class is implementing serializable interface JVM will support to convert the state of object of those classes into a byte stream
          --  Transferring the data contained in the object from RAM to hard disk is known as Serialization.
         --Transfering data through network that Object most be a serializable object that means it helps in decoding data before tranfering and encode the data after revecing.


Example :

public class Student implements Serializable {
    int id;
    String name;
    public Student(int id,String name)
    {
        this.id = id;
        this.name = name;
    }
}

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;

public class Persist {
public static void main(String[] args) throws Exception {
    Student s = new Student(3, "bala");
    try {
        FileOutputStream fout = new FileOutputStream("E:/f.txt");
        ObjectOutputStream out = new ObjectOutputStream(fout);
        out.writeObject(s);
        out.flush();
        System.out.println("Serialized data saved");
    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }
   
}
}

No comments:

Post a Comment