title: Reading Objects until End of File in java
body: <p>I'm trying to write a program where the user can: 1) Add a person to the contact (name, phone, email), 2) Remove a person from the contacts, 3) Read all from contact.</p>
  
  <p>The Way I'm doing this is I'm asking for the user for their choice and respectively does whatever. For writing, I simply write an object to the file. For removing, I think I'll be asking the user for "last name" which will be used as the KEY (since I'm using a TreeMap)and will remove the value (object) at the key.</p>
  
  <p>So I'm having a problem with reading here. I'm trying to read the object like so:</p>
  
  <pre><code>public void readContact()
  {
    TreeMap&lt;String, Contact&gt; contactMap = new TreeMap&lt;String, Contact&gt;();
    try 
    {
      ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(
                                                     new FileInputStream(file)));
  
      while( in.available() &gt; 0 ) //This line does NOT read  
      {
        Contact c = (Contact)in.readObject();
        contactMap.put(c.getLastName(), c);           
      }
  
      for(Map.Entry contact : contactMap.entrySet() ) 
      {
        Contact con = contactMap.get( contact.getKey() );
        System.out.println( con.getLastName() + ", " + con.getFirstName() + ": " + con.getPhoneNumber() + "\t" + con.getEmail());
      }
    }
    catch(Exception e)
    {
      System.out.println("Exception caught");
    } 
  }
  </code></pre>
  
  <p>Please do not suggest doing something like <code>while(true)</code> until I get the <code>EOFException</code> because:</p>
  
  <ol>
  <li>that isn't what exception handling is for I believe</li>
  <li>I still have more things to do after this so I can't have the program terminating'</li>
  </ol>