• Post Reply Bookmark Topic Watch Topic
  • New Topic

Basic Object Serialization Questions  RSS feed

 
Landon Blake
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the proper syntax to for read an object that has been serialized? (After the ObjectInputStream has been set-up.) Does it look anything like this?:
MyClass MyObject = objStream.readObject(ObjectIWantToRead)
Thanks for any information,
Landon
[ March 11, 2004: Message edited by: Landon Blake ]
[ March 11, 2004: Message edited by: Landon Blake ]
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your ObjectInputStream should be built from another InputStream, for instance a FileInputStream if you are reading the object from a file.
Call readObject()(no parameters for this method), but since readObject returns an Object, you need to either cast that object to the expected type, or deal with it as a generic Object.
Check out the
ObjectInputStream API for details.
 
Landon Blake
Ranch Hand
Posts: 121
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the response Jason. You reply has prompted a couple of more questions.
Does this mean I can only read one object from a file? What if I have multiple objects serialized to one file, but I only want to read one or two of those objects? Is there a way to dictate which object gets serialized?
[ March 11, 2004: Message edited by: Landon Blake ]
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can write multiple objects to a file. You have to know what order those objects are in, however.
For example, if you write a SomeObjectA then SomeObjectB, your code reading in the objects needs to know that order. Unless of course SomeObjectA and SomeObjectB implement the same sort of interface (whether from a superclass or an actual interface) then you can handle both generically.
What objects get written to the files and which ones don't again will be the responsibility of your code, based on whatever parameters determines what objects should be in the file.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!