Some problems reading serialized objects from a file.
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Tuesday, January 14, 2003 12:34 AM

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?

Many thanks,

José

   <<Less

Re: Some problems reading serialized objects from a file.

Posted By:   Anonymous  
Posted On:   Wednesday, January 15, 2003 01:08 AM

An additional aclaration:

I have check to us this loop:



while (objIn.available() > 0)

{

obj = (MyObject)(objIn.readObject());



...



}


But it does not work (I do not retrieve any object from the file).



José

About | Sitemap | Contact