March 23, 2023

How to read and write Java object to a file

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 interface or its subinterface,

How to read and write Java object to a file Read More

Java – How to generate serialVersionUID

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.

Java – How to generate serialVersionUID Read More

Java – What is serialVersionUID

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.

Java – What is serialVersionUID Read More

How to read an object from file in Java (ObjectInputStream)

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 and java.base/*. Others all reject !*.

How to read an object from file in Java (ObjectInputStream) Read More