title: How to read and write an object to a text file in java?
body: <p>I have an array of objects and I want to write them in a text file. So that I can later read the objects back in an array. How should I do it?
  <strong>Using Serialization.</strong></p>
  
  <p>Deserialization is not working: </p>
  
  <pre><code>public static void readdata(){
          ObjectInputStream input = null;
          try {
              input = new ObjectInputStream(new FileInputStream("myfile.txt")); // getting end of file exception here
          } catch (FileNotFoundException e1) {
              e1.printStackTrace();
          } catch (IOException e1) {
              e1.printStackTrace();
          }
          try {
              array = (players[]) input.readObject(); // null pointer exception here
          } catch (ClassNotFoundException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  
      public static void main(String[] args) {
          readdata();
          writedata();
      }
  </code></pre>