Monday, 29 September 2014

Serialization

Serialization
Def : Serialization in java is a mechanism of writing the state of an object into a byte stream.
Used In  : It is mainly used in Hibernate, RMI, JPA, EJB, JMS technologies.

 DeSerialization
Def  : The reverse operation of serialization is called deserialization.

The String class and all the wrapper classes implements java.io.Serializable interface by default.

Advantage of Java Serialization

It is mainly used to travel object's state on the network (known as marshaling).
Serializable is a marker interface (has no body). It is just used to "mark" java classes which support a certain capability.

ObjectOutputStream class 

An ObjectOutputStream is used to write primitive data types and Java objects to an OutputStream.Only objects that support the java.io.Serializable interface can be written to streams. 

Constructor

1) public ObjectOutputStream(OutputStream out) throws IOException {}:-creates an ObjectOutputStream that writes to the specified OutputStream.

Important Methods

1) public final void writeObject(Object obj) throws IOException {} :-writes the specified object to the ObjectOutputStream.
2) public void flush() throws IOException {}:-flushes the current output stream.
3) public void close() throws IOException {} :-closes the current output stream.

ObjectInputStream class

An ObjectInputStream deserializes objects and primitive data written using an ObjectOutputStream.

Constructor

1) public ObjectInputStream(InputStream in) throws IOException {} :-creates an ObjectInputStream that reads from the specified InputStream.

Important Methods

1) public final Object readObject() throws IOException, ClassNotFoundException{} :-reads an object from the input stream.
2) public void close() throws IOException {}:-closes ObjectInputStream.

 Example of Java Serialization
import java.io.Serializable;
public class Employee implements Serializable {
  public String name;

  public void mailCheck() {
    System.out.println("Mailing a check to " + name + " " + name);
  }
}


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

public class SerializeDemo {
  public static void main(String[] args) {
    Employee e = new Employee();
    e.name = "Reyan Ali";
    try {
      FileOutputStream fileOut = new FileOutputStream("employee.txt");
      ObjectOutputStream out = new ObjectOutputStream(fileOut);
      out.writeObject(e);
      out.close();
      fileOut.close();
      System.out.printf("Serialized data is saved in employee.txt");
    }
    catch (IOException i) {
      i.printStackTrace();
    }
  }
}
 O/P : Serialized data is saved in employee.txt

Example of Java Deserialization

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

public class DeserializeDemo {
  public static void main(String[] args) {
    Employee e = null;
    try {
      FileInputStream fileIn = new FileInputStream("employee.txt");
      ObjectInputStream in = new ObjectInputStream(fileIn);
      e = (Employee) in.readObject();
      in.close();
      fileIn.close();
    }
    catch (IOException i) {
      i.printStackTrace();
      return;
    }
    catch (ClassNotFoundException c) {
      System.out.println("Employee class not found");
      c.printStackTrace();
      return;
    }
    System.out.println("Name: " + e.name);
  }
}

 O/P : Name: Reyan Ali is printed in console


 

Print Friendly and PDF
Print Friendly Version of this pagePrint Get a PDF version of this webpagePDF

No comments:

Simplest way to display Server Time in JSP using js

THE BELOW CODE WORKS ONLY IN JSP : <html> <body > <table width="95%" border="1" cellpadding="1...