• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

end of ObjectInputStream

 
Sofie Deceuninck
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can you read all the objects of a file written by an ObjectOutputStream? I tried the following code but it didn't work
Thanks for any suggestions !
 
Blake Minghelli
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<hr></blockquote>
Well, that code above has one problem I can see. You are calling readObject() twice as much as you should: the first line calls it, causing the next object in the file to be read but it is not assigned to any variable. Then the next line calls it again which reads the next object in the file and assigns it to "reservatie".
Try something like this instead:

However, I was surprised to find out that after reading the last object in the file, the next readObject() call threw an exception (EOFException) instead of just returning a null. So, I'm sure there is a better way of doing this, but off the top of my head, you could try this:

[ March 12, 2004: Message edited by: Blake Minghelli ]
 
Sofie Deceuninck
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still have a problem with my output. I write some objects to my file and I ad them at the end of the file, but when I want them to read with my input (ObjectInputStream) it only gives the first object and for the rest of the objects I get an IOException. Can sombody explain me this problem?
The following code are implementated in 2 different classes.

 
Blake Minghelli
Ranch Hand
Posts: 331
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suspect this has to do with the fact that you're appending new objects to an existing file. You'd think that would work. I tried it and saw the same problem you are having. I got a CorruptedStreamException. I'll see if I can find an explanation or maybe someone else has one?
 
Sofie Deceuninck
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I solved the problem by using an ArrayList where I put all my objects in. Then I only have to write my ArrayList to the file.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic