This question has already been answered:
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);
writeBinary, and therenew ObjectOutputStream(..)in the second line. The problem is that it writes a header every time, and when reading the second object, theObjectInputStreamexpects to see the code like the next record, but seesACand stops. - zRrrreadObject.readObject()beforereadObject.readObject()from the second call to skip 4 bytes of the extra header. - zRrr