Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I did a demo and unit test for you, currently I use java.util.Date to substitute your SomeDate class .</p> <p><strong>update:</strong> 2013-12-31 </p> <p>I am not trying to make things complex,but I really feel it is my responsibility to not mislead others,so I try to fixed the code again.Currently, HashMap can't be append,please improve it.Thanks!</p> <p>this code refactored from your code:</p> <pre><code>import java.io.*; import java.util.*; /** * refactored by your code * append object stream haven't realized,please help * 2013-12-31 */ public class Reminder implements Serializable { public static void main(String[] args) { //do some initialization Reminder re = new Reminder(); re.put(new Date(System.currentTimeMillis()), "Hope it work!"); re.put(new Date(System.currentTimeMillis()+100), "it work!"); re.put(new Date(System.currentTimeMillis()+200), "Wake up!"); //save to file ,using append mode String filpath = "/tmp/reminder.ser"; re.save(filpath,true); //load from file and iterate the key-value pair Reminder reLoad = Reminder.Load(filpath); if(reLoad != null) { Iterator&lt;Map.Entry&lt;Date,String&gt;&gt; it = reLoad.entrySet().iterator(); while(it.hasNext()) { Map.Entry&lt;Date,String&gt; entry = it.next(); System.out.format("reminder: %tc---%s%n",entry.getKey(),entry.getValue()); } } } public Set&lt;Map.Entry&lt;Date,String&gt;&gt; entrySet() { return reminderMap.entrySet(); } public void put(Date chosenDate, String string) { reminderMap.put(chosenDate, string); } public String get(Date chosenDate) { return reminderMap.get(chosenDate); } /** * serializing an object * @param filePath path to save file * @param append indicate whether append or not */ public void save(String filePath,boolean append){ System.out.println("Trying to save"); try { ObjectOutputStream out = new ObjectOutputStream ( new FileOutputStream(filePath,append)); out.writeObject(this); out.close(); System.out.printf("Serialized data is saved in "+filePath); }catch(IOException e) { e.printStackTrace(); } } /** * deserialize ,load from file and rebuild object * @param filePath the path from where to load * @return a new Object */ public static Reminder Load(String filePath) { System.out.println("Trying to load"); Reminder reminder = null; try { ObjectInputStream in = new ObjectInputStream (new FileInputStream(filePath)); reminder = (Reminder) in.readObject(); in.close(); }catch(IOException | ClassNotFoundException e) { e.printStackTrace(); } return reminder; } private static final long serialVersionUID = 1L; private Map&lt;Date,String&gt; reminderMap = new HashMap&lt;&gt;(); } </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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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