title: Java StreamCorrupted Exception, using JSON
body: <p>I'm not sure exactly why I am getting this error at the bottom of the code? Any help is appreciated. I understand that is has something to do with the use of the ObjectOutputStream or ObjectInputStream? Just not entirely positive. Thanks for any help in advance.</p>
  
  <p>//MAIN </p>
  
  <pre><code>import java.io.Serializable;
  
  class LibraryOfMovieDescriptions implements Serializable {
      public static void main(String[] args){       
  
      MovieLibrary movieLibrary;
      movieLibrary = new MovieLibrary("movies.json");
  
      movieLibrary.toJsonFile("movies.json");
      }
  }
  </code></pre>
  
  <p>//MOVIE DESCRIPTION CLASS</p>
  
  <pre><code>import org.json.JSONException;
  import org.json.JSONObject;
  import org.json.JSONArray;
  import java.io.Serializable;
  
  public class MovieDescription implements Serializable {
  
  private String title;
  private String rating;
  private String release;
  private String runtime;
  private String plot;
  private String filename;
  private String genre;
  private String actors;
  
  public JSONObject toJSONObject() throws JSONException {
       JSONObject obj = new JSONObject();
       obj.put("Title", title);
       obj.put("Rated", rating);
       obj.put("Released", release);
       obj.put("Runtime", runtime);
       obj.put("Plot", plot);
       obj.put("Filename", filename);
  
  
       JSONArray a = new JSONArray();
       String[] sArray = this.actors.split(" , ");
       for(int i = 0; i &lt; sArray.length; i++){
           a.put(i);
       }
       obj.put("Actors", a);
  
       JSONArray g = new JSONArray();
       String[] gArray = this.genre.split(" , ");
       for(int i = 0; i &lt; gArray.length; i++){
           g.put(i);
       }
       obj.put("Genre", g);
  
       return obj;
  }
  
  public MovieDescription(JSONObject jsonObj) throws JSONException{               
      this.title = jsonObj.getString("Title");    
      this.rating = jsonObj.getString("Rating"); 
      this.release = jsonObj.getString("Released");
      this.plot = jsonObj.getString("Plot");
      this.runtime = jsonObj.getString("Runtime");
      this.filename = jsonObj.getString("Filename");
  
      JSONArray g = jsonObj.getJSONArray("Genre");
      for(int i = 0; i &lt; g.length(); i++){
          this.genre += g.get(i) + ", ";
      }
  
      JSONArray a = jsonObj.getJSONArray("Actors");
      for(int i = 0; i &lt; a.length(); i++){
          this.actors += a.get(i) + ", ";
      }
  }
  
  public MovieDescription(){
      title = " ";
      rating = " ";
      release = " ";
      runtime = " ";
      plot = " ";
      filename = " ";
      genre = " ";
      actors = " ";
  }
  
  public MovieDescription(String title, String rating, String release, String runtime, String plot, String filename,
                          String genre, String actors){
      this.title = title;
      this.rating = rating;
      this.release = release;
      this.runtime = runtime;
      this.plot = plot;
      this.filename = filename;
      this.genre = genre;
      this.actors = actors;
  }
  
  public void setTitle(String title){
      this.title = title;
  }
  
  public String getTitle(){
      return title;
  }
  
  public void setRating(String rating){
      this.rating = rating;
  }
  
  public String getRating(){
      return rating;
  }
  
  public void setRelease(String release){
      this.release = release;
  }
  
  public String getRelease(){
      return this.release;
  }
  
  public void setRuntime(String runtime){
      this.runtime = runtime;
  }
  
  public String getRuntime(){
      return runtime;
  }
  
  public void setPlot(String plot){
      this.plot = plot;
  }
  
  public String getPlot(){
      return plot;
  }
  
  public void setFilename(String filename){
      this.filename = filename;
  }
  
  public String getFilename(){
      return filename;
  }
  
  public void setGenre(String genre){
      this.genre = genre;
  }
  
  public String getGenre(){
      return genre;
  }
  
  public void setActors(String actors){
      this.actors = actors;
  }
  
  public String getActors(){
      return actors;
  }
  
  public String toString(){
      String string = ("Title: " + title + "\n" + "Rating: " + rating + "\n" + "Released: " + release + "\n" +
      "Runtime: " + runtime + "\n" + "Plot: " + plot + "\n" + "Filename: " +    filename + "\n" + "Genre: " + genre
      + "\n" + "Actors: " + actors + "\n");
  
          return string;
      }
  
  }
  </code></pre>
  
  <p>//MOVIE LIBRARY CLASS</p>
  
  <pre><code>import org.json.JSONObject;
  import java.io.IOException;
  import java.io.File;
  import java.util.ArrayList;
  import java.util.List;
  import java.io.FileInputStream;
  import java.io.FileOutputStream;
  import org.json.JSONTokener;
  import java.io.Serializable;
  import java.io.ObjectInputStream;
  import java.io.ObjectOutputStream;
  
  public class MovieLibrary implements Serializable {
  
  private List&lt;MovieDescription&gt; movieLib = new ArrayList&lt;MovieDescription&gt;();
  
  private int arraySize;
  
  public MovieLibrary(){
      arraySize = 0;
  }
  
  public boolean isEmpty(){
      return arraySize == 0;
  }
  
  public MovieDescription get(String aTitle){
      int i = indexOf(aTitle);
      if(i == -1){
          return null;
      }
      return movieLib.get(i);
  }
  
  public boolean add(MovieDescription aClip){
      movieLib.add(aClip);
      arraySize++;
      return true;
  }
  
  public boolean remove(String aTitle){
      int i = indexOf(aTitle);
      if(i != -1){
          movieLib.remove(i);
          arraySize--;
      }
      return true;
  }
  
  public String[] getTitles(){
      String[] s = new String[movieLib.size()];
      for(int i = 0; i &lt; movieLib.size(); i++){
          s[i] = movieLib.get(i).getTitle();
      }
      return s;
  }
  
  private int indexOf(String aTitle){
      for(int i = 0; i &lt; movieLib.size(); i++)
          if(((movieLib.get(i)).getTitle()).equals(aTitle)){
              return i;
          }
      return -1;
  }
  
  public MovieLibrary(String jsonFile){ 
  
      ObjectInputStream in = null;
      File inFile = null;
      JSONObject jsonObj = null;
      String[] titles;
  
      try{        
          inFile = new File(jsonFile);
          in = new ObjectInputStream(new FileInputStream(inFile));
          jsonObj = new JSONObject(new JSONTokener(in));
          titles = JSONObject.getNames(jsonObj);
          System.out.println("Adding Movies...");
          for(int i = 0; i &lt; titles.length; i++){
              JSONObject temp = jsonObj.getJSONObject(titles[i]);
              MovieDescription movies = new MovieDescription(temp);
              movieLib.add(movies);
          }
          System.out.println(this.getTitles());
          in.close();         
      }
      catch(Exception e){
          e.printStackTrace();
          if(in != null){
              try{
                  in.close();
              }
              catch(IOException z){
                  z.printStackTrace();
                  System.exit(0);;
              }
          }
      }
  }
  
  public void toJsonFile(String jsonFileName){
  
      JSONObject jsonObj = new JSONObject();
      ObjectOutputStream out = null;
  
      try{
          for(int i = 0; i &lt; movieLib.size(); i++)
              jsonObj.put(movieLib.get(i).getTitle(),movieLib.get(i).toJSONObject());
          out = new ObjectOutputStream(new FileOutputStream(jsonFileName));       
          out.writeObject(jsonObj.toString());
          out.close();
      }
      catch(Exception e){
          e.printStackTrace();
          if(out != null){
              try{
              out.close();
              }
              catch(IOException z){
                  z.printStackTrace();
                  System.exit(0);
              }
          }
      }
  
  
  }
  }
  </code></pre>
  
  <p>And now I am getting this error: </p>
  
  <pre><code>java.io.StreamCorruptedException: invalid stream header: 7B202022
    at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
    at java.io.ObjectInputStream.&lt;init&gt;(Unknown Source)
    at MovieLibrary.&lt;init&gt;(MovieLibrary.java:79)
    at LibraryOfMovieDescriptions.main(LibraryOfMovieDescriptions.java:11)
  </code></pre>