Serialization of Objects
- Eclipse: Oxygen
- Java: 1.8
Serialization of Objects
It is the converting of Java objects into byte streams and deserialization, as the name suggests, in a contrast to it. Stated differently, serialization is the conversion of a Java object into a static stream (sequence) of bytes which can then be saved to a database or transferred over a network.
Here we are casting the information using another kind of vehicle version id class. Whereas we are reading some other vehicle id class. When we are trying to cast the information there is some inconsistency, issues with the version id. Making use of serialization is it gives us an additional layer of consistency, so there is no inconsistency throughout the application and we are making use of the exact class for casting the information while reading the file.
Java code for serialization
In the following program, to serialize an object, we need to ensure that the class of the object implements the java.io.Serializable interface. Serializable is a marker interface; It does not declare any method and tells the serialization mechanism that the class can be serialized.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.io.Serializable; public class Vehicle implements Serializable { /** * */ private static final long serialVersionUID = -8948432491824001606L; private String type; private int number; public Vehicle(String type, int number) { this.type = type; this.number = number; } @Override public String toString() { return "Vehicle [type=" + type + ", number=" + number + "]"; } } |
Now that we have made the class eligible for serialization, the next step is really to serialize the object. This is done by calling the writeObject() method of the java.io.ObjectOutputStream class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
public class WriteObject { public static void main(String[] args) { Vehicle bike = new Vehicle("bike", 9444); Vehicle car = new Vehicle("Car", 4268); try(FileOutputStream fos = new FileOutputStream("studyeasy\\vehicles.dat")){ try(ObjectOutputStream obj = new ObjectOutputStream(fos)){ obj.writeObject(bike); obj.writeObject(car); System.out.println("Object written onto the file"); } } catch (FileNotFoundException e) { System.out.println("ileNotFoundException"); } catch (IOException e) { e.printStackTrace(); System.out.println("IOException"); } } } |
Object restoration occurs with the call to the obj.readObject() method. readObject() method can read any serializable object, a conversion to the correct type is required.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
public class ReadObject { public static void main(String[] args) { try (FileInputStream FIO = new FileInputStream("studyeasy\\vehicles.dat")) { try (ObjectInputStream obj = new ObjectInputStream(FIO)) { Vehicle v1 = (Vehicle) obj.readObject(); Vehicle v2 = (Vehicle) obj.readObject(); System.out.println("Object 1: "+v1); System.out.println("Object 1: "+v2); } catch (ClassNotFoundException e) { e.printStackTrace(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } |
Contributed by: Poonam Tomar