Hi,
I am trying to put objects in a file and later want to read them.
I am using following code to add objects in file
//for adding object in file
Java Code:
public void addObject() throws IOException, ClassNotFoundException
{
        ObjectOutputStream objs=new ObjectOutputStream(new FileOutputStream("x:\\ABCD1.dat",true));
        MyClass o1=new MyClass("abc", "abc");
        MyClass o2=new MyClass("cde", "pqr");
        objs.writeObject((Object)o1);
        objs.writeObject((Object)o2);
        objs.flush();
        objs.close();
}
And to read object from file i am using following code

Java Code:
public void getObject() throws FileNotFoundException, IOException
{
        ObjectInputStream obji=new ObjectInputStream(new FileInputStream("x:\\ABCD1.dat"));
        MyClass my=null;
        boolean flag=true;
        while(flag==true)
        {
            try
            {
                my=(MyClass)obji.readObject();
                System.out.println(""+my.str1);
                System.out.println(""+my.str2);
            }
            catch(Exception e)
            {
                System.out.println("Now in catch\n"+e);
                flag=false;
            }
        }
        obji.close();
}
This works fine if i add two object on calling a addObject() function.
But if i modify my addObject() function as shown below
Java Code:
public void addObject() throws IOException, ClassNotFoundException
 {
        //adding object inside file
        ObjectOutputStream objs=new ObjectOutputStream(new FileOutputStream("x:\\ABCD1.dat",true));
        MyClass o1=new MyClass("abc", "abc");
        objs.writeObject((Object)o1);
        objs.flush();
        objs.close();
}
Now when i call this function twice from my main() function it adds two object in file(when i viewed file externally i can see two objects there).
But when i call getObject() function it reads only one object from file and on trying to read second object it gives following error.
java.io.StreamCorruptedException: invalid type code: AC
No matter how many object i insert in a file it reads only first object and then gives above error.
I am unable to understand what is going wrong in this please help.

Thanks in advance.