This blog is all about to learn Core Java Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.
Serializable Interface
The serialization interface has no methods or fields and serves only to identify the semantics of being serializable.Serialization in java is a mechanism of writing the state of an object into a byte stream. It must be implemented by the class whose object you want to persist.
The String class and all the wrapper classes implements java.io.Serializable interface by default.
The serialization associates a version number with each serializable class called serialVersionUID ,which is used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization.
If the receiver has loaded a class for the object that has a different serialVersionUID of sender's class then deserialization will result in an InvalidClassException.
A serializable class can declare its own serialVersionUID explicitly by declaring a field named "serialVersionUID" that must be static, final, and of type long.
private static final long serialVersionUID = -7343212483392987741L;
If serializable class does not explicitly declare a serialVersionUID ,then at runtime it will take a default serialVersionUID value for that class.However it is strongly recommended that all serialiazable classes should explicitly declare serialVersionUID value, because default serialVersionUID computation is highly sensitive to class and that may vary compiler to compiler implementations, and can result in InvalidClassExceptions during deserialization.
The reverse operation of serialization is called deserialization.
The writeObject method of ObjectOutputStream class is responsible for writing the state of the object for its particular class.
The readObject method of ObjectOutputStream class is responsible for reading from the stream and restoring the classes fields.
Example of Serializable Interface :-
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
class Employee implements Serializable{
private static final long serialVersionUID = -7343212483392987741L;
private String name;
private int id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
public class SerialiazableExample2 {
static void storeObject(Employee emp,String filePath) throws IOException { //Java Serialization Method
FileOutputStream os = new FileOutputStream(filePath);
ObjectOutputStream ob = new ObjectOutputStream(os);
ob.writeObject(emp);
ob.flush();
ob.close();
System.out.println("Object Written In File Successfully.."+filePath);
}
static void displayObject(String filePath) throws IOException, ClassNotFoundException{ // Java Deserialization Method
System.out.println("READING DATA FROM FILE:::");
FileInputStream is = new FileInputStream(filePath);
ObjectInputStream ob = new ObjectInputStream(is);
Employee emp = (Employee)ob.readObject();
System.out.println("NAME\tID");
System.out.println("----\t--");
System.out.println(emp.getId()+"\t"+emp.getName());
ob.close();
}
public static void main(String[] args) throws IOException, ClassNotFoundException {
String filePath = "D:/serializable_file.txt";
Employee e = new Employee();
e.setName("Pushkar Khosla");
e.setId(101);
storeObject(e,filePath);
displayObject(filePath);
}
}
Program Output :-
Object Written In File Successfully..D:/serializable_file.txt
READING DATA FROM FILE:::
NAME ID
---- --
101 Pushkar Khosla

No comments:
Post a Comment