Wednesday, 14 August 2013
Do you like this Article?
To deserialize an object from file, first get the serialized data from the file using FileInputStream. In case if you want to know how to serialize an object to file please check this article serialize an object to a file. Pass the FileInputStream object through ObjectInputStream. Now, get the object from ObjectInputStream using the API readObject().
Suppose we have a file named sampleClass.ser which contains the data of a serialized object of class SampleClass. Please check this post to create this file. Where SampleClass is a normal pojo class with 4 variables, among which 1 is declared as transient, that means, protected from serialization.
Class SampleClass :
Now, during serialization of the object of this SampleClass, we set the following values.
Now, we execute the DeserializeFileToObject class, to deserialize the object of SampleClass.
Class DeserializeFileToObject :
Here, we can see, we get all the data from the deserialized object except strPassword, as it is decleared as transient.