title: Not able to serialize java.util.HashMap object
body: <p>I am trying to write hashmap to file and read it back. Initially, my file is empty. so, when i try to read using readObject(), it throws EOFException. I caught it and proceed with normal execution. Then, I input details of a student. Insert the details in hashmap using rollnumber as key. Then, i write the hashmap to the file using writeObject(). I check the file and it had something written in it. 
      I close the program and execute it again.
      This time it should have read the hashmap that i had written. But, instead it clears the file and still throws EOFException.
      Help me.. how should i do it???</p>
  
  <p><strong>Student.java</strong></p>
  
  <pre><code>package student;
  
  public class Student implements java.io.Serializable {
      private String name;
      private int age;
  
      class Address implements java.io.Serializable{
          String house;
          String street;
          String city;
          String state;
          int pin;
          Address(String house,String street,String city,int pin,String state){
              this.house = new String(house);
              this.street = new String(street);
              this.city = new String(city);
              this.state = new String(state);
              this.pin = pin;
          }
      }
      private Address addr;
      private int rollnumber;
      private String courses[];
  
      public Student(String name,int age,String house,String street,String city,String state,int pin,int roll, String courses[]){
          this.name = new String(name);
          this.age = age;
          this.addr = new Address(house,street,city,pin,state);
          this.rollnumber = roll;
          this.courses = new String[4];
          System.arraycopy(courses, 0, this.courses, 0, 1);
          System.out.println("NEW STUDENT CREATED..");
      }
  
      public String toString(){
          String s = rollnumber +" "+name+" "+age+"\n";
          return s;
      }
  
      int getRollNumber(){
          return rollnumber;
      }
  
  }
  </code></pre>
  
  <p>I have made both classes Student and inner class Address serializable. Tell me if there is any trouble here. </p>
  
  <p><strong>StudentRunner.java</strong></p>
  
  <pre><code>package student;
  import java.io.*;
  import java.util.*;
  
  public class StudentRunner {
      public static void main(String [] args) throws java.io.IOException, ClassNotFoundException{
          String file = "student.txt";
          StudentProcessor sp = new StudentProcessor();
          FileOutputStream fout = new FileOutputStream(file);
          FileInputStream fin = new FileInputStream(file);
          ObjectOutputStream out = new ObjectOutputStream(fout);
          ObjectInputStream in = new ObjectInputStream(fin);
  
          HashMap&lt;Integer, Student&gt; hm= new HashMap&lt;Integer, Student&gt;();
          Student stud=null;
          try{
          hm = (HashMap&lt;Integer, Student&gt;)in.readObject();
  
                  ///////// TROUBLE IN THE ABOVE LINE ///////////
  
  
          }catch(EOFException eof){
              System.out.println("sfdasfasfas");
          }
  
          Scanner scanner = new Scanner(System.in);
          int option=0;
  
          do{
              System.out.println("(1) Add User Details");
              System.out.println("(2) Display User Details");
              System.out.println("(3) Delete User Details");
              System.out.println("(4) Save User Details");
              System.out.println("(5) Exit");
              try{
                  option = scanner.nextInt();
              }catch(InputMismatchException ime){
                  System.out.println("Error!!! provide valid option(1-5).");
                  continue;
              }
  
  
          switch(option){
              case 1:
                  try{
  
                      scanner.nextLine();
  
                      System.out.println("Enter full name: ");
                      String name = scanner.nextLine();
                      System.out.println("Enter age: ");
                      int age = scanner.nextInt();
                      System.out.println("Enter rollnumber: ");
                      int rollnumber = scanner.nextInt();
  
                      scanner.nextLine();
  
                      System.out.println("Enter Address:");
                      System.out.println("  Enter house number ");
                      String house_num = scanner.nextLine();
                      System.out.println("  Enter street: ");
                      String street = scanner.nextLine();
                      System.out.println("  Enter city: ");
                      String city = scanner.nextLine();
                      System.out.println("  Enter state: ");
                      String state = scanner.nextLine();
                      System.out.println("  Enter pin: ");
                      int pin = scanner.nextInt();
  
                      String courses[] = new String[4];
                      System.out.println("Enter four courses(A-F)");
                      System.out.println("  Enter first course: ");
                      courses[0] = scanner.next();
                      System.out.println("  Enter second course: ");
                      courses[1] = scanner.next();
                      System.out.println("  Enter third course: ");
                      courses[2] = scanner.next();
                      System.out.println("  Enter fourth course: ");
                      courses[3] = scanner.next();
  
                      boolean flag = Validator.validateStudent(name, age, rollnumber, house_num, street, city, 
                                                               state, pin, courses);
  
                      stud= new Student(name,age,house_num,street,city,state,pin,rollnumber,courses);
                      if(flag){
                          hm.put(rollnumber, stud );
                      }
                  }catch(InputMismatchException ime){
                      System.out.println("Error!!! Provide numeric value..");
                      continue;
                  }
                  break;
              case 2:
                  Set set = hm.entrySet();
                  Iterator itr = set.iterator();
                  while(itr.hasNext()){
                      Map.Entry&lt;Integer, Student&gt; m =(Map.Entry&lt;Integer, Student&gt;) itr.next();
                      System.out.println(m.getValue());
                  }
                  break;
              case 3:
                  break;
              case 4:
                  out.writeObject(hm);
                  break;
              case 5:
                  break;
              default:
                  System.out.println("Error!!! provide valid option(1-5).");
                  continue;
          }
  
          if(option&gt;=5)
              break;
  
          }while(true);
      }
  }
  </code></pre>
  
  <p>hm.readObject() in StudentRunner.java, is where i am reading the content. But it clears the already written content and throws EOFException. </p>