Monday 20 October 2014

Object Serialization Deserialization java

TO store name, address,city detail and other person information are store in data base sqlite or any other database . but how we can store large size of image in to database. some of few data base structure can provide some data type but in sqlLite can provide this structure. so now i can display you that how we can store media file to store in to file.


Classes ObjectInputStream and ObjectOutputStream are high-level streams that contain the methods for serializing and deserializing an object.

The ObjectOutputStream class contains many write methods for writing various data types, but one method in particular stands out:

public final void writeObject(Object x) throws IOException

The above method serializes an Object and sends it to the output stream. Similarly, the ObjectInputStream class contains the following method for deserializing an object:

public final Object readObject() throws IOException,ClassNotFoundException

This method retrieves the next Object out of the stream and deserializes it. The return value is Object, so you will need to cast it to its appropriate data type.


To demonstrate how serialization works in Java, I am going to use the EmployeeInfo class. In Java We are calling as Plain Object.which implements the Serializable interface:


public class EmployeeInfo implements Serializable{
    public String name;
    public String address;
   public String getName() {
      return name;
   }
  public void setName(String name) {
     this.name = name;
  }
  public String getAddress() {
     return address;
  }
  public void setAddress(String address) {
    this.address = address;
  }
}



Notice that for a class to be serialized successfully, two conditions must be met:
  • The class must implement the java.io.Serializable interface.
  • All of the fields in the class must be serializable. If a field is not serializable, it must be marked transient. 

Serializing an Object:

The ObjectOutputStream class is used to serialize an Object. The following SerializeDemo program instantiates an EmployeeInfo object and serializes it to a file.

public class SerializeDemo{
   public static void main(String [] args){
      EmployeeInfo e = new EmployeeInfo ();
      e.name = "Alpan A Patel";
      e.address = "At Dived, Po Atul, Dist Valsad (Talav Dalia);
      try{
         FileOutputStream fileOut =  new FileOutputStream("/tmp/employee.bin");
         ObjectOutputStream out = new ObjectOutputStream(fileOut);
         out.writeObject(e);
         out.close();
         fileOut.close();
         System.out.printf("Serialized data is saved in /tmp/employee.bin");//you can give any extension
      }catch(IOException i){
          i.printStackTrace();
      }
   }
}

Deserializing an Object:

The following DeserializeDemo program deserializes the Employee object created in the SerializeDemo program. Study the program and try to determine its output:

import java.io.*;
public class DeserializeDemo{
   public static void main(String [] args){
      Employee e = null;
      try{
         FileInputStream fileIn = new FileInputStream("/tmp/employee.bin");
         ObjectInputStream in = new ObjectInputStream(fileIn);
         e = (EmployeeInfo) in.readObject();
         in.close();
         fileIn.close();
      }catch(IOException i){
         i.printStackTrace();
         return;
      }catch(ClassNotFoundException c){
         System.out.println("EmployeeInfo class not found");
         c.printStackTrace();
         return;
      }
      System.out.println("Deserialized Employee...");
      System.out.println("Name: " + e.name);
      System.out.println("Address: " + e.address); 
    }
}

All the Best

No comments:

Post a Comment