title: Java - How to read file into ArrayList of Objects?
body: <p>I wanna make an <code>ArrayList</code> of Student and save it to a file for later use. I successfully wrote it but when I read it back to <code>ArrayList</code>, I have only one Object.</p>
  
  <pre><code>public class Student implements Serializable{
  public String fname, lname, course;
  int section;
  private static final long serialVersionUID = 1L;
  
  public static ArrayList&lt;Student&gt; students = getStudent();
  
  public Student() {
  }
  
  public Student(String fname, String lname, String course, int section){
      this.fname = fname;
      this.lname = lname;
      this.course = course;
      this.section = section;
  }
  public static void addStudent(){
      String fname = GetInput.getInput("Enter the First Name: ");
      String lname = GetInput.getInput("Enter the Last Name: ");
      String course = GetInput.getInput("Enter the Course: ");
      String S_section = GetInput.getInput("Enter the section: ");
      int section = Integer.parseInt(S_section);
      Student student = new Student(fname, lname, course, section);  
      students.add(student); 
      System.out.println("Writing to file...");
      try {
          writeToFile(student);
      } catch (FileNotFoundException ex) {
          System.out.println(ex.getMessage());
      } catch (IOException ex) {
          System.out.println(ex.getMessage());
      }
  }
  
  public static ArrayList&lt;Student&gt; getStudent(){
      try{
          FileInputStream fis = new FileInputStream("C:\\students.ser");
          ObjectInputStream ois = new ObjectInputStream(fis);
          ArrayList&lt;Student&gt; students1 = (ArrayList&lt;Student&gt;) ois.readObject();
  
          ois.close();
  
          return students1;
      } catch( ClassNotFoundException | IOException ex){
          System.out.println(ex.getMessage());
          return null;
      }
  }
  public static void listStudent(ArrayList&lt;Student&gt; students){
      System.out.println("View the Records in the Database:");
      for(Student student: students){
          System.out.println("Name: " + student.fname + " " + student.lname);
          System.out.println("Course: " + student.course);
          System.out.println("Section: " + student.section);
          System.out.println();
      }
  }
  
  static void writeToFile(Student student) throws FileNotFoundException, IOException{
      String path = "C:\\students.ser";
      FileOutputStream fos = new FileOutputStream(path, true);
      ObjectOutputStream oos = new ObjectOutputStream(fos);
      oos.writeObject(student);
      oos.close();
      System.out.println("New Record has been written!");
  }
  </code></pre>
  
  <p>When I read file by calling <code>getStudent()</code> and print it out by <code>listStudent()</code> I have only one record of the file. </p>
  
  <p>Please help me!
  Much appreciate.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I had tried writing an <code>arraylist</code> to file and read it into <code>arraylist</code>. I'll show you how I did that.
  Firstly, I write <code>arraylist</code> to file:</p>
  
  <pre><code>public static ArrayList&lt;Student&gt; students = new ArrayList&lt;&gt;();
  
  public static void addStudent(){
      Student student = new Student(fname, lname, course, section);  
      students.add(student); 
      System.out.println("Writing to file...");
      try {
          writeToFile(students);
      }catch...
  }
  
  static void writeToFile(ArrayList&lt;Student&gt; students) throws FileNotFoundException, IOException{
      String path = "C:\\students.ser";
      FileOutputStream fos = new FileOutputStream(path, true);
      ObjectOutputStream oos = new ObjectOutputStream(fos);
      oos.writeObject(students);
      oos.close();
      System.out.println("New Record has been written!");
  </code></pre>
  
  <p>And then I read student file:</p>
  
  <pre><code>public static ArrayList&lt;Student&gt; getStudent(){
      try{
          FileInputStream fis = new FileInputStream("C:\\students.ser");
          ObjectInputStream ois = new ObjectInputStream(fis);
          ArrayList&lt;Student&gt; students1 = (ArrayList&lt;Student&gt;) ois.readObject();
          ois.close();
          return students1;
      } catch( ClassNotFoundException | IOException ex){
          System.out.println(ex.getMessage());
          return null;
      }
  }
  </code></pre>
  
  <p>I can see that in the file I have many objects as the file size keep growing. But I only one object after read it, which is my very first object I wrote to file.</p>