15

I am trying to read some objects from a file. The code works fine for the first iteration and at the second iteration it gives a StreamCorruptedException. Here is my code,

private ArrayList<Cheque> cheques = null;
ObjectInputStream ois = null;
        try {
            cheques = new ArrayList<Cheque>(4);
            ois = new ObjectInputStream(new FileInputStream("src\\easycheque\\data\\Templates.dat"));
            Object o = null;
            try {
                o = ois.readObject();
                int i=1;
                while (o != null) {
                    cheques.add((Cheque) o);
                    System.out.println(i++); // prints the number of the iteration
                    o = ois.readObject(); // exception occurs here
                }
            } catch (ClassNotFoundException ex) {// for ois readObject()
                Logger.getLogger(TemplateReader.class.getName()).log(Level.SEVERE, null, ex);

            } catch (EOFException ex) {// for ois readObject()
                // end of the file reached stop reading
                System.out.println("ois closed");
                ois.close();

            }
        } catch (IOException ex) {
            Logger.getLogger(TemplateReader.class.getName()).log(Level.SEVERE, null, ex);
        }

below is part of the exception. Before printing this '1' is printed (because of the sout)

SEVERE: null
java.io.StreamCorruptedException: invalid type code: AC
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1356)
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351) 

I can't figure out why this is happening. In few forum posts, I came across that this happens when you append to a file during writing it. Is it the real reason?. (I am appending to the file during writing phase). If so is there a proper way to read an appended file?

here is the code i use to write to the file

 ObjectOutputStream objectOut = new ObjectOutputStream(new FileOutputStream("src\\easycheque\\data\\templates.dat", true));

 objectOut.writeObject(cheque);
 objectOut.flush();
 objectOut.close();

writing is not an iterative process.

Thank you :)

Niroshan
  • 2,064
  • 6
  • 35
  • 60
  • Would you be able to add an example snippet of writing to the file as well? – Will A Jul 05 '10 at 21:28
  • Can object size be a matter? The each object I am writing has a image object (Icon) – Niroshan Jul 05 '10 at 21:51
  • It isn't correct to loop until `readObject()` returns null. It doesn't do that at end of stream, and it can do it any other time you wrote a null. You need to loop until `EOFException` is thrown. – user207421 Feb 21 '16 at 21:13

2 Answers2

16

(I am appending to the file during the writing phase)

And that is the problem. You can't append to an ObjectOutputStream. That will definitly corrupt the stream and you get StreamCorruptedException.

But I already left a solution to this problem on SO: an AppendableObjectOutputStream

EDIT

From the writer I see that you write one cheque object and flush and close the stream. From the reader, I clearly see, that you're trying to read more than one cheque object. And you can read the first one but not the rest. So to me it is perfectly clear, that you reopen the Stream and append more and more cheque objects. Which is not allowed.

You have to write all cheque objects in 'one session'. Or use the AppendableObjectOutputStream instead of the standard ObjectOutputStream.

Dr. Xperience
  • 475
  • 1
  • 5
  • 18
Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268
  • yep thats exactly what I am doing here. Thank you. Is there a some otherway to write a file the way I am doing and use a standard reading mechanism? – Niroshan Jul 05 '10 at 22:11
  • You could write all cheques to separate files - but for one file you have to write in one session. (or use my class - then you don't have to change much of your code) – Andreas Dolk Jul 05 '10 at 22:19
  • Thanks again, it seems using your class is the better solution – Niroshan Jul 05 '10 at 22:30
7

Creating a new ObjectInputStream without closing the underlying FileInputStream solves this problem:

    FileInputStream fin = new FileInputStream(file);
    while (...) {
        ObjectInputStream oin = new ObjectInputStream(fin);
        Object o = oin.readObject();
        ...
    }
    fin.close();
sfussenegger
  • 35,575
  • 15
  • 95
  • 119
  • This answer addresses the underlying issue of having used multiple ObjectOutputStream instances to write the separate cheques, so if the file is from a third party, this is the way to go – Atreys Mar 04 '16 at 17:57
  • All you need now is to magically know when to open a new `ObjectInputStream`. The solution is actually not to cause the problem in the first place. – user207421 Aug 24 '17 at 09:14