Tuesday, 17 January 2012
Do you like this Article?
By implementing an interface Serializable in the class whose instances are to be serialized.Then passing the instance to the ObjectOutputStream which is connected to a fileoutputstream. This will save the object to a file.
Now, we will see a practical implementation of serialization on an object and we will store the serialized data in a file.
Class SampleClass is a pojo class which implements java.io.Serializable . It contains four String fields, one of which is declared as transient. That means, we can't serialized this field.
Class SampleClass :
Now, Class SerializeObjectToFile, is used to create an object of SampleClass with some values.
Then we serialized that object to a file name sampleClass.ser.
Class SerializeObjectToFile :
Now, to deserialize this data back to an object please check the post Deserializing an Object from File