How to store and retrieve serialized objects to and from a file in Java? The object’s current state can be persisted and retrieved by using the concept known as ‘serialization’. This is done on an object to a stream. The stream would function as a container for the object. The persistent container, like a file on disk, allows the data of an object to be stored after the current session is completed. The interface ‘Serializable’ would support to perform the operations along with ObjectOutputStream and ObjectInputStream class objects. The method to write data is writeObject() and the method to retrieve object’s data is readObject().
|