0

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){}

?

Mazzy
  • 13,354
  • 43
  • 126
  • 207
  • This is confusing. `ObjectOutputStream` doesn't read, and doesn't have a `readObject` method. However `ObjectInputStream`'s `readObject` method doesn't take any arguments. So, something is not right in that code. – James Clark Dec 03 '11 at 22:20
  • See also http://stackoverflow.com/questions/8379326/deserializing-multiple-objects-from-a-single-file – James Clark Dec 05 '11 at 02:02

5 Answers5

2

readObject() returns null if and only if you wrote a null. The correct technique is to catch EOFException and when you get it close the stream and exit the reading loop.

user207421
  • 305,947
  • 44
  • 307
  • 483
1

Let's assume you meant ObjectInputStream and p = obj.readObject().

I would do something like this: (this is wrong, see EDIT below)

FileInputStream fstream = new FileInputStream(fileName);
try {
  ObjectInputStream ostream = new ObjectInputStream(fstream);
  while (ostream.available() > 0) {
    Object obj = ostream.readObject();
    // do something with obj
  }
} finally {
  fstream.close();
}

EDIT

I take it back! EJP rightly points out that the use of available() is incorrect here. I think the fixed code might be:

FileInputStream fstream = new FileInputStream(fileName);
try {
  ObjectInputStream ostream = new ObjectInputStream(fstream);
  while (true) {
    Object obj;
    try {
      obj = ostream.readObject();
    } catch (EOFException e) {
      break;
    }
    // do something with obj
  }
} finally {
  fstream.close();
}

Although the documentation for readObject() doesn't explicitly say that EOFException is 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.

James Clark
  • 1,765
  • 13
  • 17
  • 2
    Wrong answer. See the Javadoc. `available() > 0` means that data is available for reading without blocking. Conversely `available() == 0` indicates that no data currently available for reading without blocking, not EOF, and these are not the same thing. Downvote. – user207421 Dec 04 '11 at 09:10
  • Very good point, I've edited the answer to hopefully correct the situation. Thank you. – James Clark Dec 04 '11 at 16:48
1

If you want to read object into your program, then you have to use ObjectInputStream, not ObjectOutputStream.

And if you will store a bunch of objects, then use an appropriate Collection for writing to file and reading from it. The API documentation for readObject does not state that it will return null or throw an exception if EOF is reached. So to be on the safe side, use Collections.

You may also want to read API docs on ObjectInputStream and ObjectOutputStream.

melihcelik
  • 4,531
  • 1
  • 21
  • 25
0
Boolean i = true;
while(i) {
    try {
        System.out.println(reader.readObject());
    } catch(Exception e) {
        i = false;
        System.out.println("Dead end");
    }
}
Vikas Gupta
  • 10,779
  • 4
  • 35
  • 42
-1

Guava's Files.toByteArray does what you want so if fame in your code is a File, then

import com.google.common.io.Files;

...

byte[] fameBytes = Files.toByteArray(fame);
Mike Samuel
  • 118,113
  • 30
  • 216
  • 245
  • It doesn't do what he wants at all. It just changes the location of the problem from one place to another. Downvote. – user207421 Dec 04 '11 at 09:13