Monday, 6 February 2017

SERIALIZATION AND DESERIALIZATION IN JAVA

Serialization - It is the process of converting objects into stream of bytes and sending them to underlying OutputStream. Using Serialization we can store object state permanently in a destination. Only java.io.serializable type objects are serialized. Serializable is a marker interface, it doesn’t have any methods. It provides special permissions to jvm to serialize object. Its operation is performed by writeObject() method of ObjectOutputStream.

DeSerialization – It is the process of converting stream of bytes into original object.

Example –
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream; 
class Account implements java.io.Serializable{
          private long accno;
          private String username;
          private String password;
          private double balance;
          public long getAccno() {
                   return accno;
          }
          public void setAccno(long accno) {
                   this.accno = accno;
          }
          public String getUsername() {
                   return username;
          }
          public void setUsername(String username) {
                   this.username = username;
          }
          public String getPassword() {
                   return password;
          }
          public void setPassword(String password) {
                   this.password = password;
          }
          public double getBalance() {
                   return balance;
          }
          public void setBalance(double balance) {
                   this.balance = balance;
          }
}
public class SerialDeserial {
          public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
                   Account ac = new Account();
                   ac.setAccno(1001);
                   ac.setUsername("Rakesh");
                   ac.setPassword("12345");
                   ac.setBalance(15000);
                   ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("xyz.txt"));
                   oos.writeObject(ac);
                   oos.close();
                   ObjectInputStream ois = new ObjectInputStream(new FileInputStream("xyz.txt"));
                   Account a = (Account)ois.readObject();
                   System.out.println(a.getAccno());
                   System.out.println(a.getUsername());
                   System.out.println(a.getPassword());
                   System.out.println(a.getBalance());
                   ois.close();
          }
}

Output –
1001
Rakesh
12345
15000.0

Note – We can serialize only Serializable object. An object is said to be Serializable if and only if the corresponding class implements Serializable interface. Serializable interface present in java.io package doesn’t contain any methods. It is a marker interface. If we are trying to serialize a Serializable object we will get Runtime Exception. We can serialize multiple object, but the serialized object must be deserialized in the same order they have been done.

No comments:

Post a Comment