Serialization and Deserialization
Serialization and Deserialization
Customer.java
class Customer{
private String name;
private long crn;
private float balance;
public Customer(String name, long crn, float
balance) {
super();
this.name = name;
this.crn = crn;
this.balance = balance;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getCrn() {
return crn;
}
public void setCrn(long crn) {
this.crn = crn;
1
}
public float getBalance() {
return balance;
}
public void setBalance(float balance) {
this.balance = balance;
}
2
}
Output:
As you can see from the above output you are getting
FileNotFoundException, IOException. Now let’s handle these
exception using try-catch
try {
FileOutputStream fos = new
FileOutputStream(path);
ObjectOutputStream oos = new
ObjectOutputStream(fos);
oos.writeObject(customer);
oos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
Output:
3
After handling FileNotFoundException, IOException, you are still
getting the exception which is NotSerializableException.
It is because whenever you want to write object to the byte-stream that
time the class whose object need to created should implement the
Serializable interface
4
return balance;
}
public void setBalance(float balance) {
this.balance = balance;
}
Deserialization
Steps to do deserialization
- Create an object of FileInputStream
- Create an object ObjectInputStream
- Call readObject() to read the object from byte stream, this will return
the object you need to type cast to the type of class and store it in
Customer object
- Using the object reference print name, crn number and balance
5
}
}
Output:
Here you have got an exception because you are trying to store the object
into the variable of type Customer. Now we need to typecast object to
Customer using explicit type casting as shown below
Customer customer = (Customer)ois.readObject();
Output:
As you can see from the above output you have got
FileNotFoundException, IOException, ClassNotFound Exception now to
resolve we need to handle these exceptions using try-catch as shown
below
6
ObjectInputStream ois = new
ObjectInputStream(fis);
Customer customer =
(Customer)ois.readObject();
System.out.println(customer.getName());
System.out.println(customer.getCrn());
System.out.println(customer.getBalance());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
Output:
As you can see from the above output we have successfully read the
data from the file
7
Serialversionuid:
At the time of serialization, with every object sender side JVM will save a Unique
Identifier. JVM is responsible to generate that unique ID based on the
corresponding .class file which is present in the sender system.
Deserialization at the time of deserialization, receiver side JVM will compare
the unique ID associated with the Object with local class Unique ID i.e. JVM will
also create a Unique ID based on the corresponding .class file which is present in
the receiver system. If both unique ID matched then only deserialization will be
performed. Otherwise, we will get a Runtime Exception saying
InvalidClassException. This unique Identifier is nothing but SerialVersionUID.
Syntax:
private static final long SerialVersionUID=10l;