How to read objects from file in a loop
Hi,
I am trying to read object of perticular type into a data structure from a file. I have written objects into this file earlier. Below is the code I am using to write to file
public void CreateOrg(Organisation creatingOrg)
{
FileOutputStream fileOut,fileOut2;
ObjectOutputStream objOutStrm,objOutstrm2;
//FileInputStream fileIn;
//ObjectInputStream oInStrm;
try
{
fileOut = new FileOutputStream(creatingOrg.name+".org");
objOutStrm= new ObjectOutputStream(fileOut);
objOutStrm.writeObject(creatingOrg);
objOutStrm.flush();
objOutStrm.close();
// file containing list of available organisation
fileOut2= new FileOutputStream("Orglist.lekha",true);
objOutstrm2= new ObjectOutputStream(fileOut2);
objOutstrm2.writeObject(creatingOrg);
objOutstrm2.flush();
objOutstrm2.close();
}
catch(Exception e)
{
System.out.println("I/O ERROR");
}
}
here creatingOrg is a object of type Organisation class and this class is serializable.
and else where I am trying to read from the file "Orglist.lekha". Below is the code I am using to read back the objects
void loadOrgList()
{
String currOrg;
FileInputStream inFile;
ObjectInputStream inObj;
boolean flag=false;
Organisation temp= new Organisation();
try
{
inFile= new FileInputStream("Orglist.lekha");
inObj= new ObjectInputStream(inFile);
while(!flag)
{
System.out.println("Inside While");
System.out.println(flag);
try{
temp= (Organisation)inObj.readObject();
System.out.println(temp.name);
}
catch(EOFException eof){System.out.println(eof); flag=true;}
}
}
catch(Exception e){ System.out.println(e);}
//catch(ClassNotFoundException clss){System.out.println(clss);}
}
Now my problem is that when I try to read objects continously, I am able to read only the first written object. While writing "Orglist.lekha" append= true. so even if I have written more than one object I am not able to read it back. I am getting following error in my second run of the while() loop. Please help me with this because I am stuck here and my work is not proceeding.
Thank you and greatful any one how cares to read this
bye :WAVE:
Objects from file in a loop
Have you looked at a more direct test as the while test, rather than the flag?
The ObjectInputStream has a read() method which returns an integer representation of the next available byte - which returns -1 if you have reached the end of the file. Now, as long as when you read the next byte you do not discard that next byte, you can test for -1 at the top of the loop, then do a readObject() to get the next object, return to the top of the loop and test for the end again, then read another Object if you can, etc.
As an alternative you might be able to use the available() method to tell you that you still have stuff in the input buffer:
Code:
while ( inObj.available() != 0 )
{
Organisation temp = new Organisation();
temp = (Organisation)inObj.readObject();
dataStore.add(temp); OR
System.out.println( "The new organisation is " + temp );
}
or something like that ...
Objects from file in a loop
The declaration is there because I had forgotten that he had declared temp in the code already, before the loop ...
But I would have put the declaration inside the loop if this was, indeed, a temporary object
Objects from file in a loop
Good work! Way to stay with it and thinking to look at the problem from another angle ...!