Powered by Blogger.

Object Serialization and Deserialization

>> Friday, May 6, 2011

Working with ObjectOutputStream class and ObjectInputStream Classes

Object Serialization and Deserialization_JavabynataraJ

When ever the object values need to be stored with in a file then ObjectOutputStream class is used. When ever the object values need to be retrieved from a file then ObjectInputStream class is used.

When ever the object values need to be stored with in a file or need to be retrieved from a file then class corresponding to that object must implement the interface Serializable.
class Employee implements Serializable
With all the stream classes i,e FileInputStream, FileOutputStream, BufferedInputStream, BufferedOutputStream can't store the objects into a file or can't retrieve the objects from a file. But ObjectOutputStream will expect FileOutputStream class object as an argument.
FileOutputStream fos = new FileOutputStream("Emp.dat");
 ObjectOutputStream oos = new ObjectOutputStream(fos);
And alsoObjectInputStream will expect the object of FileInputStream class as an argument.
FileInputStream fis = new FileInputStream("Emp.dat");
 ObjectInputStream ois = new ObjectInputStream(fis); 
The writeObject() method which is present in ObjectOutputStream class is used to write object values into a file.

Object Serialization and Deserialization_JavabynataraJ

The readObject() method which is present in ObjectInputStream class is used to read the object values from a file.
readObject() will thrown an ObjectException and it will be handled.

Before doing the program checkout Serialization process and Deserialization.
To Serialize your class make sure to implement Serializable interface.
The POJO class is : "Employee.java"
The Code for above Scenario.
package javabynataraj.iopack;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

class Employee implements Serializable{
    
    String ename;
    int eno;
    float esal;
    
    Employee(String name, int no, float sal){
        ename = name;
        eno = no;
        esal = sal;
    }
    void display(){
        System.out.println(ename+"   "+eno+"    "+esal);
    }
}

public class StoreObj {
    public static void main(String[] args) {
        try{
            Employee obj = new Employee("murali", 1011, 5000);
            FileOutputStream fos = new FileOutputStream("Emp.dat");
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(obj);
            System.out.println("Serialization of Object is done..!");
            oos.close();
            fos.close();
            
            FileInputStream fis = new FileInputStream("Emp.dat");
            ObjectInputStream ois = new ObjectInputStream(fis);
            Employee obj2 = (Employee)ois.readObject();
            obj2.display();
            System.out.println("DeSerialization of Object is done..!");
            ois.close();
            fis.close();
        }catch(IOException ioe){
            ioe.printStackTrace();
        }
        catch(ClassNotFoundException cnfe){
            cnfe.printStackTrace();
        }
    }
}

Related Posts Plugin for WordPress, Blogger...
© javabynataraj.blogspot.com from 2009 - 2022. All rights reserved.