title: How to write and read java serialized objects into a file
body: <p>I am going to write multiple objects to a file and then retrieve them in another part of my code. My code has no error, but it is not working properly. Could you please help me find what is wrong about my code.
  I read different codes from different websites, but none of them worked for me!</p>
  
  <p>Here is my code to write my objects to a file:
  MyClassList is an arraylist which includes objects of my class (which must be written to a file).</p>
  
  <pre><code>for (int cnt = 0; cnt &lt; MyClassList.size(); cnt++) {
      FileOutputStream fout = new FileOutputStream("G:\\address.ser", true);
      ObjectOutputStream oos = new ObjectOutputStream(fout);
      oos.writeObject(MyClassList.get(cnt));
  }
  </code></pre>
  
  <p>I added "true" to the constructor of the outputstream, because I want to add each object to end of the file. Is that correct?</p>
  
  <p>And here is my code to read the objects from the file:</p>
  
  <pre><code> try {
       streamIn = new FileInputStream("G:\\address.ser");
       ObjectInputStream objectinputstream = new ObjectInputStream(streamIn);
       MyClass readCase = (MyClass) objectinputstream.readObject();
       recordList.add(readCase);
       System.out.println(recordList.get(i));
   } catch (Exception e) {
       e.printStackTrace();
   }
  </code></pre>
  
  <p>It finally prints out just one object. Now, I don't know if I am not writing correctly or reading correctly!</p>