dcsimg
The "available" method from "ObjectInputStream" fails reading several objects from a file
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Wednesday, January 15, 2003 06:08 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; ... sIn = new FileInputStream("ob   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;



...



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

objIn = new ObjectInputStream(sIn);



...



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.


We have checked also this loop:



while (objIn.available() > 0)

{

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



...



}


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


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: The "available" method from "ObjectInputStream" fails reading several objects from a file

Posted By:   Salar_Salmo  
Posted On:   Tuesday, February 11, 2003 06:31 PM

Hi this is my code for reading serialized Objects from a file. listeContenu is a defaultListModel

public void lireDonnees(){
try{
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("ListObjet"));
listeContenu = (DefaultListModel)ois.readObject();
ois.close();
}
catch(Exception e){
listeContenu = new DefaultListModel();
}
}

As you can see my objects are transtyped into dafaultListModel which i use to create a JList. But me too i still have a problem because all the lines of my list are filled of something like this.
Weapon@34fd4pf44
Armor@477ff7ff44
Weapon@74df4f44
And i would have prefer output like this
Sword Weapon 3
Shield Armor 5
Axe Weapon 7
So if someone have found how writing code for a lisible output he would help two guys in one shot.
About | Sitemap | Contact