my dog learned polymorphism*
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 Murach's Java Servlets and JSP this week in the Servlets 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 .

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Storing Serializable Objects in files
 
Similar Threads
Java and XML
Convertion of string object to BLOB
File Upload
xml persistence between clicks
serialization content