In this post we will learn about Serialization in Java.
Serialization is a mechanism in Java wherein we write object state into file system. It is insecure operation as compare to Object cloning.
Let us go little bit in depth to understand more.
Through this mechanism object can be represented as a sequence of bytes which includes the object’s data as well as information about the object’s type and the types of data stored in the object. After an object is serialized , the state can be read from the file.
Most interesting and impressive part of this mechanism is that this is platform independent.
Method which is used for Serialization is:
public final void writeObject(Object x) throws IOException
Let us take an example:
For reading data from the file we need to deserialize it using the method
public final Object readObject() throws IOException, ClassNotFoundException
Let us take an example for deserialization :