title: Java write and read an array of objects to file
body: <p>I have a base class named <code>Train</code>:</p>
  
  <pre><code>public abstract class Train {
      String serialNo;
      float weight;
      final String label;
  
      public Train(String serialNo, float weight, String label) {
          this.serialNo = serialNo;
          this.weight = weight;
          this.label = label;
      }
  
      public abstract float getCapacity();
  }
  </code></pre>
  
  <p>And I have 2 classes implementing the abstract class(I will not include getters and setters in the code): </p>
  
  <pre><code>public class FreightTrain extends Train implements Cloneable {
  
      private float capacityKg;
      Vector&lt;String&gt; freightVector = new Vector&lt;String&gt;();
  
      public FreightTrain(String serialNo, float weight, String label) throws Exception{
          super(serialNo, weight, label);
          if (weight &lt; 0)
              throw new Exception();
      }
  
      @Override
      public float getCapacity() {
          return this.capacityKg;
      }
  }
  </code></pre>
  
  <p>And the PassengerTrain class:</p>
  
  <pre><code>public class PassengerTrain extends Train implements Cloneable {
  
      private float seatNo;
      Vector&lt;String&gt; passengerId = new Vector&lt;String&gt;();
  
      public PassengerTrain(String serialNo, float weight, String label) throws Exception{
          super(serialNo, weight, label);
          if (weight &lt; 0)
              throw new Exception();
      }
  
      @Override
      public float getCapacity() {
          return this.seatNo;
      }
  }
  </code></pre>
  
  <p>Next I have an array list <code>ArrayList&lt;Train&gt; arr;</code> which contains both: the <code>PassengerTrain</code> and <code>FreightTrain</code>. I want to create methods to write the items from <code>arr</code> to a file and read the data from file</p>
  
  <p>Here is my attempt:</p>
  
  <pre><code>public void writeObjectInTextFile(ArrayList&lt;Train&gt; array) {
      Formatter f = null;
      try {
          f = new Formatter(file);
      } catch (FileNotFoundException e) {
          e.printStackTrace();
      }
  
      for(Train train: array) {
  
          if(train instanceof PassengerTrain) {
              String label = train.getLabel();
              String serialNo = train.getSerialNo(); 
              float capacity = train.getCapacity();
              float weight = train.getWeight();
              float seatNo = ((PassengerTrain) train).getSeatNo();
              String passId = "";
  
              for(String id:((PassengerTrain) train).getPassengerId()) {
                  passId += id;
              }
  
              f.format("%s %f %s %f %f %s", serialNo, weight, label, capacity, seatNo, passId);
          } else if(train instanceof FreightTrain) {
              String label = train.getLabel();
              String serialNo = train.getSerialNo(); 
              float capacity = train.getCapacity();
              float weight = train.getWeight();
              float capacityKg = ((FreightTrain) train).getCapacityKg();
              String freightVector = "";
  
              for(String freight: ((FreightTrain) train).getFreightVector()) {
                  freightVector += freight;
              }
  
              f.format("%s %f %s %f %f %s", serialNo, weight, label, capacityKg, capacity, freightVector);
          }
      }
      f.close();
  }
  </code></pre>
  
  <p>But I have a problem: I am unable to create a function that will read this data from the file, and return the correct ArrayList with the initial data.</p>
  
  <p>What is the best and fastest way to write the array of 2 different classes deriving from a single class to a file? </p>
  
  <p>And how it could be recreated?</p>
  
  <p>Thank you!</p>
  
  <p>Please don't my question as duplicate. I have searched for similar questions and my question is different from the ones available.</p>
  
  <p>I don't know how to convert back the objects from file to their respective types. What If I have <code>n</code> deriving classes?</p>