Saturday, May 9, 2015

Serialize and Deserialize the object in java.

Serialization is a process of converting an object into a sequence of bytes which can be persisted to a disk or database or can be sent through streams. The reverse process of creating object from sequence of bytes is called deserialization.

In this process there are three steps
1-    Create java class by implementing the Serializable class.
2-    Create SerializeDemo class to write a java object into file.
3-    Create DeserializeDemo class to read the java object from file.





Example:

Employee.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.atozjavatutorials;

public class Employee implements java.io.Serializable
{
    public static final long serialVersionUID = 42L;
    public String name;
    public String address;
    public transient int SSN;
    public int number;
    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;
    }
    public int getSSN() {
        return SSN;
    }
    public void setSSN(int SSN) {
        this.SSN = SSN;
    }
    public int getNumber() {
        return number;
    }
    public void setNumber(int number) {
        this.number = number;
    }
    public void mailCheck()
    {
      System.out.println("Mailing a check to " + name + " " + address);
    }
}

SerializeDemo.java

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package com.atozjavatutorials;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class SerializeDemo
{
   public static void main(String [] args)
   {
      Employee e = new Employee();
      e.setName("Ram Singh");
      e.setAddress("Ayodhya India");
      e.setSSN(11122333);
      e.setNumber(101);
      Employee e1 = new Employee();
      e1.setName("Shyam Sinhg");
      e1.setAddress("Mathura India");
      e1.setSSN(11155333);
      e1.setNumber(102);
      List al=new ArrayList();
      al.add(e);
      al.add(e1);
      try
      {
         FileOutputStream fileOut =
         new FileOutputStream("H:\\SerializedObjects\\employee.ser");
         ObjectOutputStream out = new ObjectOutputStream(fileOut);
         out.writeObject(al);
         out.close();
         fileOut.close();
         System.out.printf("Serialized data is saved in H:\\SerializedObjects\\employee1.ser");
      }catch(IOException i)
      {
          i.printStackTrace();
      }
   }
}

DeserializeDemo.java


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.atozjavatutorials;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class DeserializeDemo {
    public static void main(String[] args) {
        Employee e = null;
        List al = new ArrayList();
        try {
            FileInputStream fileIn = new FileInputStream("H:\\SerializedObjects\\employee.ser");
            ObjectInputStream in = new ObjectInputStream(fileIn);
            al = (List) 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("Deserialized Employee...");
        for (int i = 0; i < al.size(); i++) {
            e=(Employee)al.get(i);
            System.out.println("Name: " +e.name);
            System.out.println("Address: " + e.address);
            System.out.println("SSN: " + e.SSN);
            System.out.println("Number: " + e.number);
        }
    }
}

No comments :

Post a Comment