Friday, August 24, 2007

Adding Object Persistence to Java Applications Using Serialization

This article discusses and demonstrates how to incorporate object persistence into a Java application using the serialization mechanism in Java.

Serialization involves saving the current state of an object to a stream, and restoring an equivalent object from that stream. The stream functions as a container for the object. Its contents include a partial representation of the object's internal structure, including variable types, names, and values. The container may be transient (RAM-based) or persistent (disk-based). A transient container may be used to prepare an object for transmission from one computer to another. A persistent container, such as a file on disk, allows storage of the object after the current session is finished. In both cases the information stored in the container can later be used to construct an equivalent object containing the same data as the original. The example code in this article will focus on persistence.

Serialization allows you to save the current state of an object to a container, typically a file.

At some later time, you can retrieve the saved data values and create an equivalent object.

Depending on which interface you implement, you can choose to have the object and all its referenced objects saved and restored automatically, or you can specify which fields should be saved and restored.

Java also provides several ways of protecting sensitive data in a serialized object, so objects loaded from a serialized representation should prove no less secure than those classes loaded at application startup.

The code needed to add serialization to your application is simple and flexible.

The Sample Code for Serialize and Deserialize an Object

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Date;
class Employee implements Serializable
{
private static final long serialVersionUID = 1L;
String fName, lName, address;
double salary;
java.util.Date hireDate;
}
public class Example4serializationNdeserialization {

public static void main(String[] args) {

Employee emp = new Employee();
emp.lName = "John";
emp.fName = "Smith";
emp.salary = 50000;
emp.address = "12 main street";
emp.hireDate = new Date();

FileOutputStream fOut=null;
ObjectOutputStream oOut=null;
FileInputStream fIn = null;
ObjectInputStream oIn = null;

try{
fOut= new FileOutputStream("c:\\serialize\\NewEmployee.ser");
oOut = new ObjectOutputStream(fOut);
oOut.writeObject(emp); //serializing employee
fIn = new FileInputStream("c:\\serialize\\NewEmployee.ser");
oIn = new ObjectInputStream(fIn);
Employee emp1 = (Employee) oIn.readObject();
System.out.println(emp1);

System.out.println("An employee is serialized into c:\\serialize\\NewEmployee.ser");
}catch(IOException e){
e.printStackTrace();
}catch (ClassNotFoundException e){
e.printStackTrace();
}
finally{
try {
oOut.flush();
oOut.close();
fOut.close();
oIn.close();
fIn.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
}

0 comments:

BidVertiser