1. Lets has a Class User and implements Serializable Interface, When we serialize an class in java then serialVersionUID is very important during reading serialized data.
Note: Run this code only in java7 or remove multiexception handling catch
package com.test.serilizable;
import java.io.Serializable;
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private int userId;
private String userName;
private String fullName;
private transient int deviceId; // This field will not be participate in serialization process
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public int getDeviceId() {
return deviceId;
}
public void setDeviceId(int deviceId) {
this.deviceId = deviceId;
}
}
2. Here take another class as SerializationDemo with main method. To serialize object we need the class as ObjectOutputStream which has method writeObject to write data in for of stream lets how it works here...
package com.test.serilizable;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class SerializationDemo {
public static void main(String[] args) throws IOException {
ObjectOutputStream objectOutputStream = null;
try {
User user = new User();
user.setUserId(1);
user.setUserName("ramu");
user.setFullName("Ramesh Chandra Srivastava");
user.setDeviceId(1001);
File dir = new File("C:/Serialization");
if (!dir.exists()) {
dir.mkdir(); // Create directory(folder) in C drive if doesn't exist
}
objectOutputStream = new ObjectOutputStream(new FileOutputStream(
dir + "/user.ser"));
objectOutputStream.writeObject(user);
System.out.println("Object has been written successfully!!!!!!");
} catch (Exception ex) {
ex.printStackTrace();
} finally {
objectOutputStream.close();
System.out.println("ObjectOutputStream has been closed successfully");
}
}
}
3. Out put
Object has been written successfully!!!!!!
ObjectOutputStream has been closed successfully
4. Now see to to deserialize data from serialized to accomplish this process we need ObjectInputStream class which has method readObject used to reform object again
package com.test.serilizable;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
public class DeserializationDemo {
public static void main(String[] args) throws IOException {
ObjectInputStream objectInputStream= null;
try{
objectInputStream = new ObjectInputStream(new FileInputStream("C:/Serialization/user.ser"));
User user= (User) objectInputStream.readObject();
System.out.println(user.getUserId() +" "+user.getUserName()+" "+user.getUserName() +" "+ user.getDeviceId());
} catch (FileNotFoundException | ClassNotFoundException ex){
ex.printStackTrace();
} finally{
objectInputStream.close();
System.out.println("ObjectInputStream has been closed");
}
}
}
5 Out put
1 ramu ramu 0
ObjectInputStream has been closed
6. Here the deviceId is has value 1001 and still in output is 0 means the transient fields aren't serialized.
What is role of serialVersionUID here in User class lets have some practical. After serialized Here i am updating User class and comment the serialVersionUID lets see what is its difference...
package com.test.serilizable;
import java.io.Serializable;
public class User implements Serializable {
//private static final long serialVersionUID = 1L;
private int userId;
private String userName;
private String fullName;
private transient int deviceId;
private String address; // Recently addded after seralized class
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public int getDeviceId() {
return deviceId;
}
public void setDeviceId(int deviceId) {
this.deviceId = deviceId;
}
}
6. Executing DeserializationDemo again
package com.test.serilizable;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
public class DeserializationDemo {
public static void main(String[] args) throws IOException {
ObjectInputStream objectInputStream= null;
try{
objectInputStream = new ObjectInputStream(new FileInputStream("C:/Serialization/user.ser"));
User user= (User) objectInputStream.readObject();
System.out.println(user.getUserId() +" "+user.getUserName()+" "+user.getUserName() +" "+ user.getDeviceId());
} catch (FileNotFoundException | ClassNotFoundException ex){
ex.printStackTrace();
} finally{
objectInputStream.close();
System.out.println("ObjectInputStream has been closed");
}
}
}
7. Output
ObjectInputStream has been closedException in thread "main"
java.io.InvalidClassException: com.test.serilizable.User; local class incompatible: stream classdesc serialVersionUID = 1, local class serialVersionUID = 7164343529204429975
at java.io.ObjectStreamClass.initNonProxy(Unknown Source)
at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at com.test.serilizable.DeserializationDemo.main(DeserializationDemo.java:14)
Oh!!! what is here an Exception, So serialVersionUID really important in serialization its check the version of the class, If you write the serialVersionUID the there is no error means it tell the JVM that the updated User class is new version of User class.
Hope you understand the serialization process in java.
Thank You
It really helped me a lot.
ReplyDeleteThanks for your updates