We have declared an MyObject class, which can be serialized: public class MyObject implements Serializable { byte campo1[] = new byte[10]; String campo2; int campo3; } And we have two java applications. One must write several serialized objects in a file and the other must read them from the file. The number of object that are being stored in the file is not know by the application. We use this java code to read from the file: FileInputStream sIn = null; ObjectInputStream objIn = null; try { sIn = new FileInputStream("
More>>

We have declared an MyObject class, which can be serialized:

public class MyObject implements Serializable

{

byte campo1[] = new byte[10];

String campo2;

int campo3;

}

And we have two java applications. One must write several serialized
objects in a file and the other must read them from the file.
The number of object that are being stored in the file is not know by
the application.

We use this java code to read from the file:

FileInputStream sIn = null;

ObjectInputStream objIn = null;

try

{

sIn = new FileInputStream("objectsFile.dat");

objIn = new ObjectInputStream(sIn);

}

catch ( ... ) { ... }

MyObject obj = null;

try

{

while ((obj = (MyObject)(objIn.readObject())) != null)

{

// Shows values in the retrieved object.

...

}

}

catch ( ... ) { ... }

The application reads all the objects from the file and shows them
into the screen, but the problem is that when the end of the file is
reached, the application ends with an exception.

Please, can you tell me how can we check for the end of the file
without finish with an exception?