title: Java - "invalid stream header" with Serialization of ArrayList
body: <p>I have a trouble with my java project. After several searches, i'm still blocked.</p>
  
  <p>I have a class named <code>ListFromFile</code> that extends <code>ArrayList</code> :</p>
  
  <pre><code>public class ListFromFile&lt;T&gt; extends ArrayList&lt;T&gt; implements Serializable {
  
      private String listFile;
  
      public ListFromFile() {
          this.listFile = null;
      }
  
      public ListFromFile(String file) {
          this.listFile = file;
      }
  
      public void loadDataFromFile() { // Deserialize from file
  
          try {
              FileInputStream fileInput = new FileInputStream(this.listFile);
  
              if ( fileInput.read() == -1 ) { // If file is empty
                  this.saveDataInToFile();
              } else {
                  ObjectInputStream objectInput = new ObjectInputStream(fileInput);
  
                  this.addAll((ArrayList) objectInput.readObject());
  
                  objectInput.close();
              }
  
              fileInput.close();
          } catch ( IOException | ClassNotFoundException e ) {
              e.printStackTrace();
          }
      }
  
      public void saveDataInToFile() { // Serialize the ArrayList
          try {
              FileOutputStream fileOutput = new FileOutputStream(this.listFile, true);
              ObjectOutputStream objectOutput = new ObjectOutputStream(fileOutput);
  
              objectOutput.writeObject(this);
  
              objectOutput.flush();
              objectOutput.close();
              fileOutput.close();
          } catch ( IOException e ) {
              e.printStackTrace();
          }
      }
  
  }
  </code></pre>
  
  <p>In my main class <code>BZ2MServer</code>, i've created some objects with ListFromFile :</p>
  
  <pre><code>public class BZ2MServer {
  
      private static ListFromFile&lt;Specialite&gt; specialites = new ListFromFile&lt;Specialite&gt;("liste_specialites.txt");
      private static ListFromFile&lt;Module&gt; modules = new ListFromFile&lt;Module&gt;("liste_modules.txt");
      private static ListFromFile&lt;Professeur&gt; professeurs = new ListFromFile&lt;Professeur&gt;("liste_professeurs.txt");
      private static ListFromFile&lt;Etudiant&gt; etudiants = new ListFromFile&lt;Etudiant&gt;("liste_etudiants.txt");
      private static ListFromFile&lt;Note&gt; notes = new ListFromFile&lt;Note&gt;("liste_notes.txt");
  
      public static void main(String[] args) {
          specialites.loadDataFromFile();
      }
  }
  </code></pre>
  
  <p>When the application starts, it loads the lists saved in files. That's why i execute <code>saveDataInToFile()</code> inside that <code>loadDataFromFile()</code> method.
  By this way, the files should contain empty ArrayList at the first time we launch the application.</p>
  
  <p>The result is that for the first launch, there is no issue. However, after this, i always have this from the compiler :</p>
  
  <pre><code>java.io.StreamCorruptedException: invalid stream header: ED000573
      at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:804)
      at java.io.ObjectInputStream.&lt;init&gt;(ObjectInputStream.java:299)
      at bz2m.list.ListFromFile.loadDataFromFile(ListFromFile.java:31)
      at bz2m.server.BZ2MServer.main(BZ2MServer.java:22)
  </code></pre>
  
  <p>I have tested this in the main of <code>BZ2MServer</code>:</p>
  
  <pre><code>public static void main(String[] args) {
      Specialite test = new Specialite(1, "Test");
  
      specialites.add(test2);
  
      specialites.saveDataInToFile();
  }
  </code></pre>
  
  <p>Then, i removed the previous code, used <code>LoadDataFromFile()</code> to get the previous datas. And i got the error. Then, i removed that condition in <code>ListFromFile</code> class :</p>
  
  <pre><code>if ( fileInput.read() == -1 ) {
      this.saveDataInToFile();
  }
  </code></pre>
  
  <p>And it worked. However, that condition was necessary in the case the file is empty. Without it, and if the file is empty, i get this :</p>
  
  <pre><code>java.io.EOFException
      at java.io.ObjectInputStream$PeekInputStream.readFully(ObjectInputStream.java:2325)
      at java.io.ObjectInputStream$BlockDataInputStream.readShort(ObjectInputStream.java:2794)
      at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:801)
      at java.io.ObjectInputStream.&lt;init&gt;(ObjectInputStream.java:299)
      at bz2m.list.ListFromFile.loadDataFromFile(ListFromFile.java:31)
      at bz2m.server.BZ2MServer.main(BZ2MServer.java:22)
  </code></pre>
  
  <p>I hope that i have give you all the information you need to help me. If you need, i can upload the entire project (not huge).</p>
  
  <p>Thank you !</p>