Note that there are some explanatory texts on larger screens.

plurals
  1. POReading Objects until End of File in java
    primarykey
    data
    text
    <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>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload