Java Mysterious EOF exception with readObject

I had the same mysterious EOFException and it was only the path of the Object Class to send across the ObjectOutputStream to the ObjectInputStream. They must have the same path (same package name and, of course, same class name).


Depends on how many objects your file contains. If it has only one object, you can deserialise in one step.

try {
    Object temp = ois.readObject();
}
catch(Exception e) {
    //handle it
}

First of all, readObject() only returns null if you wrote null to the stream when creating it. If there is no more data in the stream, it will throw an EOFException.

If you don't expect the EOF, the reason is probably that the stream is corrupt. This can happen if you forget to close it after writing data to it.