What is the purpose of serialization in java?
Serialization is a conversion of an object to a series of byte, so that the object can be easily saved to persistent storage or streamed across a communication link, The byte stream can then be deserialized - converted into a replica of an original object.
Serialization is a conversion of an object to a series of byte, so that the object can be easily saved to persistent storage or streamed across a communication link, The byte stream can then be deserialized - converted into a replica of an original object.
Transient : If any variable is defined by using keyword “transient”, then object will not serialize that variable. During deserialization default value of that datatype will be set.
Static : as static variables are class level variables, so it is not serialzed as the object state.
We can do the Serialization process by implementing Serializable interface.
class SerialDemo implements java.io.Serializable
The above step tells the compiler that you are going to implement serialization.
Which basically means that the object is going to be saved or persisted.
use FileOutputStream class and ObjectOutputStream class.
FileOutuptStream Creates an output file stream to write to the file with the specified name. A new FileDescriptor object is created to represent this file connection.
An ObjectOutputStream writes primitive data types and graphs of Java objects to an OutputStream. The objects can be write using an ObjectOutputStream.
Save the file using
writeObject(obj);
Program for Serialization
What if you have something top secret, like your password which should not be saved. You mark it as transient!
transient int i;
Create a class Employee to collect information.
package com.javabynataraj.iopack;
class Employee implements java.io.Serializable {
public String name;
public String address;
public transient int SSN;
public int number;
public void mailCheck() {
System.out.println("Mailing a check to " + name + " " + address);
}
}
then create a main class to persist the Employee details in a file.
package com.javabynataraj.iopack;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class Serial {
public static void main(String[] args) {
Employee e = new Employee();
e.name = "Muralidhar";
e.address = "JavabynataraJ,SatyaTechnologies";
e.SSN = 11111;
e.number = 101;
try {
System.out.println("Before Serialization");
FileOutputStream fileOut = new FileOutputStream("employee.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(e);
out.close();
fileOut.close();
System.out.println("End of Serialization");
} catch (IOException i){
i.printStackTrace();
}
}
}
Deserialization is a process of converting the data from files or database converting to Stream of bytes using class Objects.
The Deserialization can be done after serializing the data only.Then we can read the data from the serialized files.
Convert your Serialized file into file form.
File fromFile = new File("Emp.ser");
By using the below two classes we can do the process of deserialization.
#1. FileInputStream
#2. ObjectInputStream
A FileInputStream obtains input bytes from a file in a file system. What files are available depends on the host environment.
FileInputStream fis = new FileInputStream(fromFile);
An ObjectInputStream deserializes primitive data and objects previously written using an ObjectOutputStream. This can be Converts the Serialized file into the Object form.
ObjectInputStream ois = new ObjectInputStream(fis);
To get the object of a serialized file we have to typecast into our class Employee to read the values in a file using variable names.
Employee emp = (Employee) ois.readObject();
After that close all the object connections.
You should write the Employee class already written in Serialization.(check it there)
Program for Deserialization:
package javabynataraj.iopack;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
public class Deserial {
public static void main(String arg[]) {
File fromFile = new File("Emp.ser");
try {
FileInputStream fis = new FileInputStream(fromFile);
ObjectInputStream ois = new ObjectInputStream(fis);
Employee emp = (Employee) ois.readObject();
System.out.println("Deserialized data: \n"+ emp.eno + " "+ emp.ename +" "+ emp.esal+" "+emp.eaddr+ " from Emp.ser");
ois.close();
} catch(IOException e) {
System.out.println("Deserialization failed");
System.out.println(e);
System.exit(1);
} catch(ClassNotFoundException e) {
System.out.println("Deserialization failed");
System.out.println(e);
System.exit(1);
}
}
}
Referance: shivasoft.in and javabynataraj.blogspot.in

No comments:
Post a Comment