Serialization:
Serialization is a mechanism where an object can be represented as a sequence of bytes that includes the object's data as well as information about the object's type and the types of data stored in it
provide a mechanism to write the state of and class objects in file in a sequence of bytes.After a
serialized object has been written into a file, it can be read from the file and deserialized that is, the type information and bytes that represent the object and its data can be used to recreate the object in memory.
Note: Most impressive is that the entire process is JVM independent, meaning an object can be serialized on one platform and deserialized on an entirely different platform.
Classes ObjectInputStream and ObjectOutputStream are high-level streams that contain the methods for serializing and deserializing an object.
The ObjectOutputStream class contains many write methods for writing various data types, but one method in particular stands out:
Syntex:
public final void writeObject(Object x) throwsIOException
The above method serializes an Object and sends it to the output stream.Similarly, the ObjectInputStream class contains the following method for deserializing an object:
Syntex:
public final Object readObject() throwsIOException, ClassNotFoundException
This method retrieves the next Object out of the stream and deserializes it.The return value is Object, so you will need to cast it to its appropriate data type.
To demonstrate how serialization works in Java below is the Example of it.
Serializing an Object:
The ObjectOutputStream class is used to serialize an Object. The below SerializableExample program instantiates an Employee object and serializes it into employee.ser file.
save as Employee.java
public class Employee implements Serializable
{
public int id;
public String name;
public String address;
public float salary;
public transient String PAN;
Employee(int id,String name,String address,float salary,String PAN)
{
this.id=id;
this.name=name;
this.address=address;
this.salary=salary;
this.PAN=PAN;
}
}
Note:For a class to be serialized successfully, two conditions must be met:
>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.
If you want to know that a Java Standard Class is serializable or not, check the documentation for that class. The test is simple: If the class implements java.io.Serializable, then it is serializable; otherwise, it's not.
Note: By using javap tool you can check that a class is serializable or not with cmd (command prompt).
save as SerializableExample.java
import java.io.*;
public class SerializableExample {
public static void main(String []args) {
Employee e =new Employee(111,"Vishwa","Gurgaon", 28000,"8086PQSE");
try {
FileOutputStream fileOut = new FileOutputStream("D:\\employee.ser");
ObjectOutputStream out= new ObjectOutputStream(fileOut);
out.writeObject(e);
System.out.println("successfully objects serialized ");
out.close();
fileOut.close();
}
catch(IOException ex) {
ex.printStackTrace();
}
}
}
Output: successfully objects serialized
When this program executed, a file named employee.ser in the D directory is created in the present working directory.The program does not generate any output.
Note: When serializing an object to a file,the standard convention in Java is to give the file
as .ser extension.
Deserilization: It is vice-versa(opposite) process of Serilization.In this concept we construct object from the serialized file.
Deserilizing an Object:
The ObjectInputStream class is used to deserialize an Object. The following DeserializableExample program instantiates an Employee object and deserializes it from employee.ser file.
Save as DeserializableExample.java
import java.io.*;
public class DeserializableExample {
public static void main(String []args) {
Employee e =null;
try {
FileInputStream fileIn = new FileInputStream("D:\\employee.ser");
ObjectInputStream in=new ObjectInputStream(fileIn);
e =(Employee)in.readObject();
in.close();
fileIn.close();
}
catch (IOException ex) {
ex.printStackTrace();
}
catch(ClassNotFoundException c) {
System.out.println("Employeeclassnot found");
c.printStackTrace();
}
System.out.println("Deserialized Employee objects...");
System.out.println("ID: "+ e.id);
System.out.println("Name: "+ e.name);
System.out.println("Address: "+ e.address);
System.out.println("Salary: "+ e.salary);
System.out.println("PAN: "+ e.PAN);
}
}
Ouput:
ID: 111
Name: Vihswa
Address: Gurgaon
Salary: 28000
PAN: null
Here are some important points to be noted:
>1. The try/catch block tries to catch a ClassNotFoundException, which is declared by the readObject() method. For a JVM to be able to deserialize an object, it must be able to find the bytecode for that class. If the JVM can't find a class during the deserialization of an object, it throws a ClassNotFoundException.
>2. Notice that the return value of readObject() is cast to an Employee reference.
>3. The value of the PAN field was 8086PQSE when the object was serialized, but because that field(PAN) is transient variable,so this value was not sent to the output stream.The PAN field of the deserialized Employee object is null.(if PAN variable's type is int then output will be 0 default value)
Serialization is a mechanism where an object can be represented as a sequence of bytes that includes the object's data as well as information about the object's type and the types of data stored in it
provide a mechanism to write the state of and class objects in file in a sequence of bytes.After a
serialized object has been written into a file, it can be read from the file and deserialized that is, the type information and bytes that represent the object and its data can be used to recreate the object in memory.
Note: Most impressive is that the entire process is JVM independent, meaning an object can be serialized on one platform and deserialized on an entirely different platform.
Classes ObjectInputStream and ObjectOutputStream are high-level streams that contain the methods for serializing and deserializing an object.
The ObjectOutputStream class contains many write methods for writing various data types, but one method in particular stands out:
Syntex:
public final void writeObject(Object x) throwsIOException
The above method serializes an Object and sends it to the output stream.Similarly, the ObjectInputStream class contains the following method for deserializing an object:
Syntex:
public final Object readObject() throwsIOException, ClassNotFoundException
This method retrieves the next Object out of the stream and deserializes it.The return value is Object, so you will need to cast it to its appropriate data type.
To demonstrate how serialization works in Java below is the Example of it.
Serializing an Object:
The ObjectOutputStream class is used to serialize an Object. The below SerializableExample program instantiates an Employee object and serializes it into employee.ser file.
save as Employee.java
public class Employee implements Serializable
{
public int id;
public String name;
public String address;
public float salary;
public transient String PAN;
Employee(int id,String name,String address,float salary,String PAN)
{
this.id=id;
this.name=name;
this.address=address;
this.salary=salary;
this.PAN=PAN;
}
}
Note:For a class to be serialized successfully, two conditions must be met:
>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.
If you want to know that a Java Standard Class is serializable or not, check the documentation for that class. The test is simple: If the class implements java.io.Serializable, then it is serializable; otherwise, it's not.
Note: By using javap tool you can check that a class is serializable or not with cmd (command prompt).
save as SerializableExample.java
import java.io.*;
public class SerializableExample {
public static void main(String []args) {
Employee e =new Employee(111,"Vishwa","Gurgaon", 28000,"8086PQSE");
try {
FileOutputStream fileOut = new FileOutputStream("D:\\employee.ser");
ObjectOutputStream out= new ObjectOutputStream(fileOut);
out.writeObject(e);
System.out.println("successfully objects serialized ");
out.close();
fileOut.close();
}
catch(IOException ex) {
ex.printStackTrace();
}
}
}
Output: successfully objects serialized
When this program executed, a file named employee.ser in the D directory is created in the present working directory.The program does not generate any output.
Note: When serializing an object to a file,the standard convention in Java is to give the file
as .ser extension.
Deserilization: It is vice-versa(opposite) process of Serilization.In this concept we construct object from the serialized file.
Deserilizing an Object:
The ObjectInputStream class is used to deserialize an Object. The following DeserializableExample program instantiates an Employee object and deserializes it from employee.ser file.
Save as DeserializableExample.java
import java.io.*;
public class DeserializableExample {
public static void main(String []args) {
Employee e =null;
try {
FileInputStream fileIn = new FileInputStream("D:\\employee.ser");
ObjectInputStream in=new ObjectInputStream(fileIn);
e =(Employee)in.readObject();
in.close();
fileIn.close();
}
catch (IOException ex) {
ex.printStackTrace();
}
catch(ClassNotFoundException c) {
System.out.println("Employeeclassnot found");
c.printStackTrace();
}
System.out.println("Deserialized Employee objects...");
System.out.println("ID: "+ e.id);
System.out.println("Name: "+ e.name);
System.out.println("Address: "+ e.address);
System.out.println("Salary: "+ e.salary);
System.out.println("PAN: "+ e.PAN);
}
}
Ouput:
ID: 111
Name: Vihswa
Address: Gurgaon
Salary: 28000
PAN: null
Here are some important points to be noted:
>1. The try/catch block tries to catch a ClassNotFoundException, which is declared by the readObject() method. For a JVM to be able to deserialize an object, it must be able to find the bytecode for that class. If the JVM can't find a class during the deserialization of an object, it throws a ClassNotFoundException.
>2. Notice that the return value of readObject() is cast to an Employee reference.
>3. The value of the PAN field was 8086PQSE when the object was serialized, but because that field(PAN) is transient variable,so this value was not sent to the output stream.The PAN field of the deserialized Employee object is null.(if PAN variable's type is int then output will be 0 default value)
No comments:
Post a Comment