title: Save Arraylist of Objects to External File Java
body: <p>I have an arrayList set up to hold player data. Each player contains several elements, namely jersey number, first name, last name, preferred position, goals, and assists. I want to be able to save this data so that when I restart the program the data is still in the exact same format. From some stuff that I have read it looks like serializing is the way to go? Is this correct or is there a better way? I am new to Java so any examples would be great. I have attached several applicable snippets of my code.</p>
  
  <p>//creation of arrayList</p>
  
  <pre><code>public class LeagueDatabase extends javax.swing.JFrame {
       ArrayList &lt;blackTeam&gt; blackTeam = new ArrayList &lt;blackTeam&gt;();
  </code></pre>
  
  <p>//how class is structured</p>
  
  <pre><code>class blackTeam {
      int goals, assists;
      String jerseyNum, firstName, lastName, prefPosition;
  
      blackTeam (String _jerseyNum, String _firstName, String _lastName, String _prefPosition, int _goals, int _assists) {
          jerseyNum = _jerseyNum;
          firstName = _firstName;
          lastName = _lastName;
          prefPosition = _prefPosition;
          goals = _goals;
          assists = _assists;
      }
  }
  </code></pre>
  
  <p>//how data is added</p>
  
  <pre><code>black = new blackTeam(jerseyNum, firstName, lastName, prefPosition, goals, assists);
  blackTeam.add(black);
  </code></pre>
  
  <p>//what I have so far for saving the data</p>
  
  <pre><code> addWindowListener(new WindowAdapter() {
          public void windowClosing(WindowEvent e) {
              try{
                  FileOutputStream fos= new FileOutputStream("blackTeam");
                  ObjectOutputStream oos= new ObjectOutputStream(fos);
                  oos.writeObject(blackTeam);
                  oos.close();
                  fos.close();
              }catch(IOException ioe){
                  ioe.printStackTrace();
              }
              //close program
              System.exit(0);
          }
      });
  </code></pre>
  
  <p>If someone could explain how to save the data to a file, and then re-load the data from that file when the program is started, that would be great. </p>