• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

doubt with serialization

 
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Moieen Khatri
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 144
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is just the beginning There are other great folks.

Deepak
 
Do not meddle in the affairs of dragons - for you are crunchy and good with ketchup. Crunchy tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic