Java (HashMap) - How to save and load an entrySet()? -


i have 2 hashmaps , want both saved when menu button clicked, , want able load them (in example i'm saving one).

void savehash(hashmap<string, object> hash, hashmap<string, object> logicalmatrix2) {     jframe parentframe = new jframe();      jfilechooser filechooser = new jfilechooser();     filechooser.setdialogtitle("especifique um diretório:");     filechooser.setacceptallfilefilterused(false);      int userselection = filechooser.showsavedialog(parentframe);      if (userselection == jfilechooser.approve_option) {         file filetosave = new file(filechooser.getselectedfile() + ".alg");         set<entry<string, object>> entry = hash.entryset();         filewriter outfile;         try {             outfile = new filewriter(filetosave.getabsolutepath());             printwriter out = new printwriter(outfile);             out.println(entry);             out.close();         } catch (ioexception e) {             // todo auto-generated catch block             e.printstacktrace();         }        } } 

with can save entryset() in txt file, have no idea of how load content back. software start hashmap empty, , then, through loading process, want able fill keys , respective values accordingly. but, considering saved in txt file, how run "reading loops" through identify unknown keys? problem gets more complicated when consider value of each key arraylist.

thank much!

potentially simplest route use objectoutputstream instead of printwriter , can read results using objectinputstream.
assuming objects contained in hashmap either serializable or externalizable.
serialize hashmap instead of entryset. there number of considerations (versioning, transient objects, enum constants, etc.) when using object serialization, suggest do reading subject.

xmlencoder , xmldecoder may allow more portability if ever need read files outside java environment or want them human readable.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -