title: Read objects from binary files in java
body: <p>I have function to serialize object and store it in a binary file, and also another object just to count how many objects I have stored on the file so far, so 
  I can use the counter later to go through the file and deserialize it and read it.
  Here is my write function:</p>
  
  <pre><code>Class2 dad = new Class2(DadName.getText(), DadLastName.getText());
  
  Class1 son = new Class1(FirstName.getText(), dad, "Session");
  
  // Write object to  file
  FileOutputStream outStream = new FileOutputStream(new File("MainStore.dat"), true);
  ObjectOutputStream objectOutputFile = new ObjectOutputStream(outStream);
  objectOutputFile.writeObject(son);
  objectOutputFile.close();
  
  
  // Update the objectcounter file by 
  // Maintenance is a class I use just to save object contain information about number of files,increase them, decrease them.
  
  Maintenance check = new Maintenance();
  FileInputStream inStream = new FileInputStream("Counter.dat");
  ObjectInputStream objectInput = new ObjectInputStream(inStream);
  check = (Maintenance) objectInput.readObject();
  inStream.close();
  
  check.increaseObject();
  
  FileOutputStream outStream1 = new FileOutputStream("Counter.dat");
  ObjectOutputStream objectOutputFile1 = new ObjectOutputStream(outStream1);
  
  objectOutputFile1.writeObject(check);
  objectOutputFile1.close();
  </code></pre>
  
  <p>The previous function works just fine, the next step is function to read read the objects back from file, I use Counter.dat to see the number of Objects stored on MainStore.dat and it gives me the right number.
  Here is the read function:</p>
  
  <pre><code>// Read Counter file to know how many stored objects to go through them
  Maintenance check = new Maintenance();
  FileInputStream inStream = new FileInputStream("Counter.dat");
  ObjectInputStream objectInput = new ObjectInputStream(inStream);
  check = (Maintenance) objectInput.readObject();
  int Counter1 = check.getObjectsNumber(); // function I created in the Maintenance class to return number of object stored
  inStream.close();
  
  
  // Read the stored objects
  //here is my problem begin
  
  FileInputStream inStream2 = new FileInputStream("MainStore.dat");
  ObjectInputStream objectInputFile = new ObjectInputStream(inStream2);
  
  // Create array of objects
  Class1[] arrayOfObjects = new Class1[Counter1];
  
  // Read the serialized objects from the file.
  for (int i = 0; i &lt; Counter1; i++) {
   arrayOfObjects[i] = (Class1) objectInputFile.readObject(); // here is the error pointed by compiler
  }
  objectInputFile.close();
  
  for (int i = 0; i &lt; 2; i++) {
   // here I should have array of all objects ready to read details
  
  }
  </code></pre>
  
  <p>Everything looks fine except the last bit "// Read the serialized objects from". it gives me this error</p>
  
  <pre><code>Caused by: java.io.StreamCorruptedException: invalid type code: AC
      at java.io.ObjectInputStream.readObject0(Unknown Source)
      at java.io.ObjectInputStream.readObject(Unknown Source)
      at registerController.CheckPlaceAvailabilityAction(registerController.java:120)
      ... 58 more
  </code></pre>
  
  <p>and When I tested it with only reading one object, it worked perfect and return the first stored object for me, the error happened when I try to read all stored objects.</p>