java. io. StreamCorruptedException: invalid type code: AC
See English answer > StreamCorruptedException: invalid type code: AC1
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,ex); }
The following are some of the exceptions Print "1" (because of South) before printing
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 don't know why this happened In several forum posts, I found that this happens when writing files Is this the real reason? I added it to the file at the writing stage Is there a correct way to read additional files?
Here is the code I use to write files
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:)
Solution
That's the problem You cannot append to objectoutputstream This will explicitly break the flow and get a StreamCorruptedException
But I have solved this problem: an appendableobjectoutputstream
edit
From the author I see you write a check object and flush and close the flow From the reader's point of view, I clearly see that you are trying to read multiple check objects You can read the first but not the rest So it's very clear to me that you reopen the stream and attach more and more check objects Which is not allowed
You must write all check objects in one session Or use an appendableobjectoutputstream instead of the standard objectoutputstream