0

I want to be able to read an object from a file after I streamed one out to it. Now, It works fine when I am reading at the first time, but when I'm trying to read again (to another object), there is an exception I'm unable to handle.

Now, I'm guessing the file index got to the end of the file, therefore I cannot read again from it.

Am I wrong? If not, can I set the file index to the start of the file?

try{
    Classba cb=new Classba();
    FileOutputStream fos=new FileOutputStream(args[0]);
    ObjectOutputStream oos=new ObjectOutputStream(fos);
    oos.writeObject(cb);
    FileInputStream fis=new FileInputStream(args[0]);
    ObjectInputStream ois=new ObjectInputStream(fis);
    Classba cb2;
    cb2=(Classba)ois.readObject();
    cb2.print();   
    Classba cb3;          //*OK Till Here*//
    cb3=(Classba)ois.readObject();
}
Menno
  • 12,175
  • 14
  • 56
  • 88
izac89
  • 3,790
  • 7
  • 30
  • 46

1 Answers1

0

You can call the method reset() on your ObjectInputStream.

Étienne Miret
  • 6,448
  • 5
  • 24
  • 36