aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt with serialization Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt with serialization" Watch "doubt with serialization" New topic
Author

doubt with serialization

Moieen Khatri
Ranch Hand

Joined: Nov 27, 2007
Posts: 144
Hi,

Can someone please tell me whether the serialized object can be saved only on a .ser file? Can I use a file with .txt extension for saving the state of instance variables? Also, What are the other methods of saving the object state?


Thanks in advance!!
Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
You can give any name to the file. Just try it out and you will come to know.

Have a look at ObjectOutStream class API doc @ ObjectoutputStream for more details.

The only method to save a object in one shot is writeObject().
But ObjectOutStream class has other methods such as write() methods where you will have to make sure your saving each member in the same order in which you will later access using the read().
Thanks
Deepak
[ November 29, 2007: Message edited by: Deepak Jain ]

SCJP, SCWCD, SCBCD
Moieen Khatri
Ranch Hand

Joined: Nov 27, 2007
Posts: 144
Hi Deepak,

Thanks for your reply. Now, I can save the state of the object on any type of file(I tried .txt file).

What I don't understand clearly is the following sentence in italic :

The objects must be read back from the corresponding ObjectInputstream with the same types and in the same order as they were written

Please can you give some example which explains this?

Many Thanks!!!
Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637


The objects must be read back from the corresponding ObjectInputstream with the same types and in the same order as they were written:
Here i first wrote Dog and then Cat.
So I must read from the file in the same order first Dog and then Cat. Otherwise you will get a ClassCastException at runtime which makes sense.

Offcourse it must be read back with same type that you had written. Say you write a Dog and then you cant write a code that will try to read a cat.
For example this will throw a ClassCastException

test.writeDog(os);
Cat c = test.readCat(is);
Hope this helps.
Thanks
Deepak
Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
Few points to keep in mind while serialization are
When you want to serialize mulitple objects you should not close the ObjectOutputStream or else only the last object that you attempted to write will be actually written. Each time ObjectOutPutSteam will create a new file and if the file exists it will delete the existing file.

Also the class that needs to be serialized must implement Serializable interface and if the class has references to other classes then they also must implement Serializable or be declared as volatile doing otherwise would cause a java.io.NotSerializableException at runtime .
Thanks
Deepak
[ November 29, 2007: Message edited by: Deepak Jain ]
Moieen Khatri
Ranch Hand

Joined: Nov 27, 2007
Posts: 144
Thanks for such an elaborative example This forum is really cool

I now understand that its the order in which the data is written to the stream which is important. The example shows that Dog data is written first to the OutPutStream and then cats data is written to the Outputstream and while reading from the Inputstream the order is maintained.

However if I change the order of reading like the below italic :

Cat c = test.readCat(is);
System.out.println(c.getCode());
System.out.println(c.getName());
Dog d = test.readDog(is);
System.out.println(d.getCode());
System.out.println(d.getName());


then the Dog object is found which cannot be casted to a cat reference and I get ClassCastException which emphasizes the importance of order while writing and reading.

Many Thanks once again!!!
Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
This is just the beginning There are other great folks.

Deepak
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt with serialization