This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes Storing Serializable Objects  in files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Storing Serializable Objects  in files" Watch "Storing Serializable Objects  in files" New topic
Author

Storing Serializable Objects in files

Anish Yadav
Greenhorn

Joined: Apr 30, 2001
Posts: 1
Can anyone tell me how to store a object into a file and later on restore it.
Matt Senecal
Ranch Hand

Joined: Dec 01, 2000
Posts: 255
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).]


There's nothing more dangerous than a resourceful idiot. ---Dilbert
surya_java
Greenhorn

Joined: Apr 27, 2001
Posts: 6
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

Joined: Dec 01, 2000
Posts: 255
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 .

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Storing Serializable Objects in files