my dog learned polymorphism*
The moose likes Swing / AWT / SWT and the fly likes Writing objects to files and reading them back into the UI Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Writing objects to files and reading them back into the UI" Watch "Writing objects to files and reading them back into the UI" New topic
Author

Writing objects to files and reading them back into the UI

Hector Rivera
Greenhorn

Joined: Feb 08, 2003
Posts: 3
I'm new to the java scene, and I need help on how to Write objects to files and reading them back into the UI.
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Hector,
Java makes it almost too easy to persist objects to a file. The classes your are interested in are ObjectOutputStream and ObjectInputStream.
Here's an example:

Note that the objects that you write to the object stream must implement either Serializable or Externalizable.
Hope this helps,
Michael Morris


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
ali rafiq
Ranch Hand

Joined: Jan 09, 2003
Posts: 75
i have also a Question about this class
we are saving an object in a test.out ..
Do we save two or more than two object in test.out file. ?
if they save more then two object. then how do we call second or another object?


if u learn to break the rock .. next day u will be looking to Mountain.
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi ali,
You keep calling readObject(), readInt(), readUTF(), or whatever. You just have to be sure that you read everything back in the same order that they wer written.
Hope this helps,
Michael Morris
 
jQuery in Action, 2nd edition
 
subject: Writing objects to files and reading them back into the UI