In a software project, in many instances, there are necessities to transfer the data, and it can be handled by using ObjectOutputStream and ObjectInputStream from Java IO packages. Usually, the data is written in binary format and hence we cannot view the contents. Serialization is the process of writing an object to an output stream. We can write a class object itself, which contains a combination of primitive datatype and graphs of Java objects. Deserialization is the process of reconstructing an object from an input stream. Show Implementation: Let us see the ObjectOutputStreamwith an is illustrated for which we create a POJO class called “VehicleSpecifications”.
Example 1: Java
Example 2: Java
Note: On execution of the above code, we can see that the data is written in a binary format that means, it is a non-readable format only. So In order to get the data in a readable format, we are using ObjectInputStream and as we have to retrieve the class object data(i.e. VehicleSpecifications data), we need to use readObject() method, which can read class attributes using read() methods provided by ObjectInputStream. Example 3: Java
Output: Output explanation: So, we are seeing the output in a human-readable format. So our entire set of class objects can be portable using ObjectInput and ObjectOutput streams provided by java.io packages. Hence, now let us finally discuss out the differences between ObjectOutputStream and ObjectInputStream after having an internal workflow understanding of them. They are illustrated below in a tabular format shown as below:
|