I have to read a series of object from a binary file.
I use:
ObjectOutputStream obj = new ObjectOutputStream(new FileInputStream(fame));
obj.readObject(p);
where p is a reference to an object I had created. How can I read the entire file until the end?
I can use:
while(p!=null){}
?
Let’s assume you meant
ObjectInputStreamandp = obj.readObject().I would do something like this: (this is wrong, see EDIT below)
EDIT
I take it back! EJP rightly points out that the use of
available()is incorrect here. I think the fixed code might be:Although the documentation for
readObject()doesn’t explicitly say thatEOFExceptionis thrown at the end of the stream, it seems to be implied and may be the only way to detect the end of the stream.Another option if you control the code that wrote the stream would be to write an object count at the beginning, or a flag after each object indicating whether the previous object was the final one.