Since Java is pure object oriented programming , all things must be done using the help of the objects. Some times if developer wants to transfer the object from one JVM to another JVM or one network to another network ,
its very difficult to pack or unpack the object contents, because the object can have any type of datatypes or methods like images ,videos etc .. , serialization helps to do this task.
Object Serialization
Object Serialization is the process of converting object into stream of bytes which would be transferred via network. In other end the stream of bytes would be converted into original object , this process is called object de-serialization. The stream of bytes will be stored into a file in local disk after serialization.Now lets take small review about classes and methods used for Serialization and De-serialization process.
Package Name : java.io
Classes :
- ObjectOutputStream
- ObjectInputStream
- FileOutputStream
- FileInputStream
Methods :
- Object readObject()
- void writeObject(Object obj)
Example Program
Now lets create one Employee Object with some fields then we can apply the serialization concept.
1. Employee.java
package com.rmi.serial;
/*
* Author :Anthoniraj.A
* Date : 30/06/09
gram For Object Creation
*/
* Description : Pr
o
import java.io.Serializable;
public class Employee implements Serializable
{
String name;
String designation;
double salary;
int exp;
}
2.Employee Serialization
package com.rmi.serial;
/*
* Author :Anthoniraj.A
* Date : 30/06/09
gram For Object Serialization
*/
* Description : Pr
o
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
public class EmpSerializable
{
Employee emp = null;;
ObjectOutputStream oos = null;
private void setValues()
{
emp = new Employee();
emp.name = "Antony";
emp.designation="AssistantProfessor";
emp.salary =32000;
emp.exp = 3;
}
private void serializeValues()
{
try
{
oos = new ObjectOutputStream(new FileOutputStream(new File("sample.ser")));
oos.writeObject(emp);
System.out.println("An Employee Object is serialized into sample.ser file");
}
catch (IOException ex)
{
ex.printStackTrace();
}
finally
{
try
{
oos.flush();
oos.close();
}
catch (IOException ex)
{
Logger.getLogger(EmpSerializable.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
public static void main(String a[])
{
EmpSerializable es = new EmpSerializable();
es.setValues();
es.serializeValues();
}
}
OutPut
An Employee Object is serialized into sample.ser file
3.Employee DeSerialization
package com.rmi.serial;
/*
* Author :Anthoniraj.A
* Date : 30/06/09
gram For Object Deserialization
*/
* Description : Pr
o
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
public class EmpDeserializable
{
ObjectInputStream ois = null;
private void getValues()
{
try
{
ois = new ObjectInputStream(new FileInputStream(new File("sample.ser")));
Employee em = (Employee) ois.readObject();
System.out.println("Employee Name :" + em.name);
System.out.println("Employee Designation :" + em.designation);
System.out.println("Employee Salary :"+em.salary);
System.out.println("Employee Experience :" + em.exp);
}
catch (Exception ex)
{
ex.printStackTrace();
}
finally
{
try
{
ois.close();
}
catch (IOException ex)
{
ex.printStackTrace();
}
}
}
public static void main(String a[])
{
new EmpDeserializable().getValues();
}
}
output
Employee Name :Antony
Employee Designation :AssistantProfessor
Employee Salary :32000.0
Employee Experience :3
No comments:
Post a Comment