In last section, you learn about how to write or serialized an object into a file. In this example , you can do more than just serialized it , you also can compress the serialized object to reduce the file size. The idea is very simple, just using the “GZIPOutputStream” for the data compression.
Java object Serialization is an API provided by Java Library stack as a means to serialize Java objects. Serialization is a process to convert objects into a writable byte stream. Once converted into a byte-stream, these objects can be written to a file. The reverse process of this is called de-serialization. A Java object is serializable if its class or any of its superclasses implement either the java.io.Serializable interface or its subinterface, java.io.Externalizable.
This article shows how to convert an object to byte or byte array and vice versa in Java. The example show how to use ByteArrayOutputStream and ObjectOutputStream to convert an object to byte. The example show how to use ByteArrayInputStream and ObjectInputStream to convert byte back to an object.
In Java, transient fields are excluded in the serialization process. In short, when we save an object into a file (serialization), all transient fields are ignored.
This article shows you a few ways to generate the serialVersionUID for serialization class. JDK has a built-in command serialver to generate a serialVersionUID automatically. For Eclipse IDE, move the mouse over the serialization class, or click on the serialization class and press CTRL + 1. In IntelliJ IDEA, we need to enable this auto-generate serialVersionUID option manually.
In Java, serialVersionUID is something like version control, assure both serialized and deserialized objects are using the compatible class. For example, if an object saved into a file (Serialization) with serialVersionUID=1L, when we convert the file back to an object (Derialization), we must use the same serialVersionUID=1L, otherwise an InvalidClassException is thrown.
In Java, Serialization means converting Java objects into a byte stream; Deserialization means converting the serialized object’s byte stream back to the original Java object. In Java, we have to implement the Serializable interface to supports serialization and deserialization.
This example shows how to use ObjectInputStream to read a serialized object from a file in Java, aka Deserialization. The below example converts a Person object to bytes stream and saves it into a file (Serialization). Later, it reads the bytes stream from the same file and converts it back to the original object (Deserialization). In Java, deserialization from untrusted byte streams is extremely dangerous. So, Java 9 introduced deserialization filters to filter the incoming serialization data. The below example shows how to use deserialization filters to only deserializing objects from com.favtuts.io.object.Person and java.base/*. Others all reject !*.
This example shows how to use ObjectOutputStream to write objects to a file in Java, aka Serialization. We can serialize or marshal an object which implements a Serializable interface. We can save object into a file, convert object to OutputStream, Convert object to byte.