• Post Reply Bookmark Topic Watch Topic
  • New Topic

Storing Serializable Objects in files

 
Anish Yadav
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone tell me how to store a object into a file and later on restore it.
 
Matt Senecal
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This isn't too difficult. When writing the objects to a file you need to make sure that their integrity as java objects is retained. This is done through serialization. An object that is serializable can be successfully serialized (converted to a serial format) and unserialized.
To write an object to a file, all you need to do is something like this:

To read the object from the file you just do the opposite:

That's all there is to it!
Originally posted by Anish Yadav:
Can anyone tell me how to store a object into a file and later on restore it.

[This message has been edited by Matt Senecal (edited April 30, 2001).]
[This message has been edited by Matt Senecal (edited April 30, 2001).]
 
surya_java
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey matt!
every thing iss fine with ur code. but dont u think the object class should implement serializable interface ?.
surya .
 
Matt Senecal
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's true, but the question wasn't about the type of object being serialized.
Originally posted by surya_java:
hey matt!
every thing iss fine with ur code. but dont u think the object class should implement serializable interface ?.
surya .

 
The only taste of success some people get is to take a bite out of you. Or this tiny ad:
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!