Note that there are some explanatory texts on larger screens.

plurals
  1. POSerializing objects with hashMap
    primarykey
    data
    text
    <p>This is my first time i try objects serializing. My problem is that when i call for saving new objects(Reminder.java objects) it saves them in the hash map but when i load it gives me the properties of the last saved object.</p> <p>So my question is:</p> <p>1.Saving - How do i "append" objects to a file ? </p> <p>2.Loading - how to iterate through them and get the right object (using the key class type MyDateClass) . Example will be welcomed. Thank you.</p> <pre><code>public void save(MyDateClass chosenDate, String string){ System.out.println("Trying to save"); reminderMap.put(chosenDate, string); //serializing an object : this.dateReminder = chosenDate; this.reminder = string; try { FileOutputStream fileOut = new FileOutputStream("/tmp/reminder.ser"); ObjectOutputStream out = new ObjectOutputStream(fileOut); out.writeObject(this); out.close(); fileOut.close(); System.out.printf("Serialized data is saved in /tmp/reminder.ser. "); }catch(IOException i) { i.printStackTrace(); } } public String Load(MyDateClass chosenDate){ System.out.println("Trying to load"); this.reminder = reminderMap.get(chosenDate); System.out.println(this.reminder); // deserialize Reminder e = null; try { FileInputStream fileIn = new FileInputStream("/tmp/reminder.ser"); ObjectInputStream in = new ObjectInputStream(fileIn); e = (Reminder) in.readObject(); in.close(); fileIn.close(); }catch(IOException i) { i.printStackTrace(); }catch(ClassNotFoundException c) { c.printStackTrace(); } return e.reminder; } } </code></pre>
    singulars
    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.
    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