I know that there were already such questions, but after reading everything I could not understand the problem.

java.io.StreamCorruptedException: invalid type code: AC at java.base/java.io.ObjectInputStream$BlockDataInputStream.readBlockHeader(Unknown Source) at java.base/java.io.ObjectInputStream$BlockDataInputStream.available(Unknown Source) at java.base/java.io.ObjectInputStream.available(Unknown Source) at ua.TaskIO.readBinary(TaskIO.java:80) at ua.Main.main(Main.java:70) 

Here is an example of my code for writing and reading from a file.

 public void writeBinary(TaskList tasks, File file) { try { ObjectOutputStream writeObject = new ObjectOutputStream(new FileOutputStream(file, true)); for (int i = 0; i < tasks.size(); i++) { writeObject.writeObject(tasks.getTask(i)); writeObject.flush(); } writeObject.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public void readBinary(TaskList tasks, File file) { try { ObjectInputStream readObject = new ObjectInputStream(new FileInputStream(file)); while (readObject.available() > -1) { Task task = (Task) readObject.readObject(); tasks.add(task); } readObject.close(); System.out.println("good"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } 

And the main method

 TaskIO io = new TaskIO(); File file = new File("text.dat"); io.writeBinary(newT, file); io.writeBinary(b, file); ArrayTaskList list = new ArrayTaskList(); io.readBinary(list, file); 

Reported as a duplicate by participants of zRrr , LFC , aleksandr barakin , 0xdb , Sergey Gornostaev java Feb 6 at 8:41 am

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • I found a lot of similar questions, but I can't understand where I re-call ObjectOutputStream that it overwrites the header to 0xAC - Vadim Volin
  • You call writeBinary , and there new ObjectOutputStream(..) in the second line. The problem is that it writes a header every time, and when reading the second object, the ObjectInputStream expects to see the code like the next record, but sees AC and stops. - zRrr
  • Alternatively, you can try readObject.readObject() before readObject.readObject() from the second call to skip 4 bytes of the extra header. - zRrr
  • Understand the mistake. Thank you. - Vadim Volin am

2 answers 2

The available method returns the number of currently readable bytes. With the condition readObject.available() > -1 reading is performed even when there is nothing to read (the available number of bytes is 0).

    I propose to keep the number of objects in the stream.

     public void writeBinary(TaskList tasks, File file) { try { ObjectOutputStream writeObject = new ObjectOutputStream(new FileOutputStream(file, true)); writeObject.writeInt(tasks.size()); for (int i = 0; i < tasks.size(); i++) { writeObject.writeObject(tasks.getTask(i)); } writeObject.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public void readBinary(TaskList tasks, File file) { try { ObjectInputStream readObject = new ObjectInputStream(new FileInputStream(file)); int count = readObject.readInt(); for (int i =0; i < count; i++) { Task task = (Task) readObject.readObject(); tasks.add(task); } readObject.close(); System.out.println("good"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } 

    PS: No need to call flush after saving each object. It only slows down the recording.

    • Made through DataInputStream / outputStream. I tried to write to-stism but the reading did not occur. - Vadim Volin