title: How to read and write Java ArrayList Objects to and from a file that has a specific required structure
body: <p>I need to store and read ArrayList Objects to a file, which that itself isn't the issue.  I need to store it with a specific format and have it have a "header" of sorts while still having each ArrayList Object be usable from the file. Another part to it, is it needs to be readable by opening the text file itself, so no serialization can be used (Unless I'm just severely mistaken on how to use serialization). <strong>Example of how the working file should look below (Figure 1).</strong></p>
  
  <p><img src="https://i.imgur.com/YPdS4nW.jpg" alt="Figure 1"></p>
  
  <p><strong>I will include all my code below just so nothing important isn't show on accident.</strong></p>
  
  <p><strong><em>Airline.java</em></strong></p>
  
  <pre><code>public class Airline extends Application {
  
  @Override
  public void start(Stage stage) throws Exception {
      Parent root = FXMLLoader.load(getClass().getResource("Airline.fxml"));
      Scene scene = new Scene(root);
      stage.setTitle("Seat Reservation");
      stage.setResizable(false);
      stage.setScene(scene);
      stage.show();
  }
  
  public static void main(String[] args) {
      Passenger p1 = new Passenger(0001, "John Smith", "1A", "AA12");
      Passenger p2 = new Passenger(0002, "Annah Smith", "1B", "AA12");
      //creating arraylist
      ArrayList &lt;Passenger&gt; pList = new ArrayList &lt;Passenger&gt;();
      pList.add(p1);
      pList.add(p2);
  
      try {
          FileOutputStream fos = new FileOutputStream(new 
          File("reservations.txt"));
          ObjectOutputStream oos = new ObjectOutputStream(fos);
          oos.writeObject(pList);
          oos.close();
          fos.close();
          FileInputStream fis = new FileInputStream(new 
          File("reservations.txt"));
          ObjectInputStream ois = new ObjectInputStream(fis);
          ArrayList list = (ArrayList) ois.readObject();
          System.out.println(list.toString());
          ois.close();
          fis.close();
  
      } catch (FileNotFoundException e) {
          System.out.println("File not found");
      } catch (IOException e) {
          System.out.println("Error initializing stream");
      } catch (ClassNotFoundException e) {
          e.printStackTrace();
      }
  
      launch(args);
      }
  
  }
  </code></pre>
  
  <p><strong><em>Passenger.java</em></strong></p>
  
  <pre><code>public class Passenger implements Serializable {
  
      private static final long serialVersionUID = 1L;
  
      private int id;
      private String name;
      private String seat;
      private String flight;
  
      Passenger() {
      };
  
      public Passenger (int idP, String nameP,String seatP, String flightP) {
  
         this.id = idP;
         this.name = nameP;
         this.seat = seatP;
         this.flight = flightP;
  
      }
  
      @Override
      public String toString() {
  
         return "\n" + id + " " + name + "    " + seat + "    " + flight;
  
      }
  
  }
  </code></pre>
  
  <p><strong>The code I have currently shows this when opening the text file (Figure 2 below).</strong></p>
  
  <p><img src="https://i.imgur.com/uwgAxu7.jpg" alt="Figure 1"></p>
  
  <p>If anyone has any suggestions please let me know! I've been stumped for quite a while now.</p>
  
  <p><em>Also</em>, if this breaks any rules or doesn't have the proper tags, let me know and I'll remove/edit it.</p>