Java
Serialization Lesion 02
Object
Serialization –
objects
represents as a sequence of bytes.
This
includes the object's data, informations about the objects type, and
the types of data stored in th e object.
Serialized
object can read once it deserialized and recreate the object in the
memory.
This
serialization and deserialization process is JVM independent.
(
object can serialized in one platform and can deserialized on another
platform )
ObjectInputStream
class - for serializing an object
-
public final void writeObject(Object x) throws IOException
ObjectOutputStream
class - for deserializing an object
-
public final Object readObject() throws IOException,
ClassNotFoundException
Since
by using serialization, Java object can write into a file for future
access.
A
class to be serialized successfully
1.
The class must implement the java.io.Serializable interface.
2.
All of the fields in the class must be serializable. If
a field is not serializable, it must be marked transient.
Example
of serialize and deserialize process -
i)
Serializing an Object:
The
ObjectOutputStream class is used to serialize an Object. Following
program instantiates an Employee object and serializes it to a
file.
When
the program is done executing, a file named employee.ser is
created.
Note:
When serializing an object to a file, the
standard convention in Java is to give the file a .ser
extension.
ii)
Deserializing an Object:
Use
.ser to deserializes and create the Employee object
1.
Employee class
package
JavaSerialize;
import
java.io.Serializable;
public
class
Employee implements
Serializable {
/**
*
*/
private
static
final
long
serialVersionUID
= 1L;
private
String firstName;
private
String lastName;
private
int
age;
private
transient
int
SSN;
private
String designation;
private
String jobTitle;
private
String project;
//TODo
generate getters and setters
}
1.
DemoSerializDeSerialize class
package
JavaSerialize;
import
java.io.FileInputStream;
import
java.io.FileOutputStream;
import
java.io.IOException;
import
java.io.ObjectInputStream;
import
java.io.ObjectOutputStream;
public
class
DemoSerializeDeSerialize {
private
int
EXECUTE =
1; // 0 - Serialize | 1 - Deserialize
/**
* @param
args
*/
public
static
void
main(String[] args) {
DemoSerializeDeSerialize
demo = new
DemoSerializeDeSerialize();
demo.execute();
}
private
void
execute(){
if(EXECUTE
== 0){
doSerialize();
}else{
doDeSerialize();
}
}
private
void
doSerialize(){
Employee
employee = buildEmployee();
try{
FileOutputStream fileOut = new
FileOutputStream("/home/sanjeeva/Desktop/employee.ser");
ObjectOutputStream out = new
ObjectOutputStream(fileOut);
out.writeObject(employee);
out.close();
fileOut.close();
System.out.printf("Serialized
data is saved in /home/sanjeeva/Desktop/employee.ser");
}catch(IOException
ioe){
ioe.printStackTrace();
}
}
private
void
doDeSerialize(){
Employee
employee = null;
try{
FileInputStream
fileIn = new
FileInputStream("/home/sanjeeva/Desktop/employee.ser");
ObjectInputStream
in = new
ObjectInputStream(fileIn);
employee
= (Employee) in.readObject();
in.close();
fileIn.close();
}catch(IOException
ioe){
ioe.printStackTrace();
return;
}catch(ClassNotFoundException
c){
System.out.println("Employee
class not found");
c.printStackTrace();
return;
}
System.out.println("Deserialized
Employee...");
System.out.println("First
Name: " + employee.getFirstName());
System.out.println("Last
Name: " + employee.getLastName());
System.out.println("SSN:
" + employee.getSSN());
System.out.println("Designation:
" + employee.getDesignation());
System.out.println("JobTitle:
" + employee.getJobTitle());
System.out.println("Project:
" + employee.getProject());
}
private
Employee buildEmployee(){
Employee
employee = new
Employee();
employee.setFirstName("Sanjeeva");
employee.setFirstName("Pathirana");
employee.setAge(30);
employee.setDesignation("Senior
Software Engineer");
employee.setJobTitle("Team
Lead");
employee.setProject("ABC
Project");
employee.setSSN(100123);
return
employee;
}
}
By
changing the EXECUTE
parameter can do the
serialization and deserialization.
There
is no out put for serialization and it creates the .ser file.
Serialized
data is saved in /home/sanjeeva/Desktop/employee.ser
The
deserialization process the out put as follows.
Deserialized
Employee...
First
Name: Sanjeeva
Last
Name: Pathirana
SSN:
0
Designation:
Senior Software Engineer
JobTitle:
Team Lead
Project:
ABC Project
Note
: The value of the SSN field was 100123
when the object was serialized, but because the field is transient,
this value was not sent to the output stream. The SSN field of the
deserialized Employee object is 0.