Results 1 to 6 of 6
- 08-15-2011, 05:35 AM #1
ObjectInputStream and the Serializable class
Hey all
I'm never used Serialization before so I thought I'd play around to increase my knowledge. I have managed to write objects out but have an issue when reading the objects back in.
All the code examples I have found only write one object out and read one object back in. How do you read in multiple objects? The OIS class doesn't seem to have any methods to determine if there are more objects to read or if you have reached EOF. Am I being dense or are you supposed to just write out the List containing all your objects instead of the individual objects themselves?
- 08-15-2011, 07:58 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
I'm no expert but I have always assumed that the reader and writer have to agree on a "protocol" - ie the reader has to know what it is reading (how many things and in what order). You can serialise a list if that's appropriate in which case the reader can read the whole thing and discover how many objects there are.
- 08-15-2011, 08:02 AM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
And - from memory, since I'm on a mobile device and searching is painful - doesn't Oracle's Tutorial example serialise *several* objects which are then read back in again?
- 08-15-2011, 08:37 AM #4
Notice that DataStreams detects an end-of-file condition by catching EOFException, instead of testing for an invalid return value. All implementations of DataInput methods use EOFException instead of return values.
I found the above in the tutorial for DataStreams. So it looks like if you do not know beforehand how many objects to read in, you have to catch the EOF exception. Which I consider somewhat unsavoury.
- 08-15-2011, 10:44 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
I don't hitnk I've ever serilized multiple objhects that weren't themselves wrapped in some other parent.
I'm trying to think of an instance and having trouble...
- 08-15-2011, 01:46 PM #6
Similar Threads
-
I need to keep a whole file as a serializable class field
By urbanleg in forum New To JavaReplies: 1Last Post: 07-10-2011, 04:33 PM -
ObjectInputStream issue
By aianta in forum New To JavaReplies: 8Last Post: 04-13-2011, 01:22 AM -
ObjectInputStream does not initialize
By Singing Boyo in forum New To JavaReplies: 1Last Post: 06-03-2009, 08:11 AM -
Reading from ObjectInputStream
By deepthought015 in forum NetworkingReplies: 8Last Post: 04-28-2009, 05:57 PM -
Stops when opening ObjectInputStream
By Norberhuis in forum NetworkingReplies: 4Last Post: 01-09-2009, 04:19 PM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks