File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes I/O and Streams and the fly likes end of ObjectInputStream Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "end of ObjectInputStream" Watch "end of ObjectInputStream" New topic
Author

end of ObjectInputStream

Sofie Deceuninck
Greenhorn

Joined: Feb 20, 2004
Posts: 11
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

Joined: Sep 13, 2002
Posts: 331
<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 ]

Blake Minghelli<br />SCWCD<br /> <br />"I'd put a quote here but I'm a non-conformist"
Sofie Deceuninck
Greenhorn

Joined: Feb 20, 2004
Posts: 11
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

Joined: Sep 13, 2002
Posts: 331
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

Joined: Feb 20, 2004
Posts: 11
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.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: end of ObjectInputStream