java - Saving Preferences from within a JAR -


i writing game, , have come point need user able save preferences things such jframe size, key bindings etc. users running game through jar downloaded website.

i've decided go preferences api under "java.util.prefs.preferences". still quite new when comes reading things documentation, , explain bit me.

so far, have this:

preferences prefs = preferences.usernodeforpackage(com.custardgames.horde2d.game.class); prefs.put(name, value); 

now then, how saving preference file on users computer? right now, if run program again , try just:

preferences prefs = preferences.usernodeforpackage(com.custardgames.horde2d.game.class); system.out.println(prefs.get(name, null)); 

it returns null default value if there nothing saved. know missing vital part of saving file, , opening correct way, haven't gotten out of googling it.

thanks in advance!

edit: fyi, reading images, using this:

bufferedimage currentimage=imageio.read(this.getclass().getclassloader().getresource("res/images/image.jpg")); 

i able similar preferences.

edit: name , value both string variables declared beforehand.

are sure name variable has same value? because should return correct stored value - maybe value null?

try printing them out before storing & getting.

you can try flush preferences after storing:

prefs.flush(); 

if you're on windows can check if preferences stored in regedit under

\hkey_current_user\software\javasoft\prefs\<your package path> 

in linux i'm not sure there should hidden folder in home directory. like

~/.java/.prefs/<package path> 

regedit preferences

i've developed jframe using preferences storing last position & size of frame. can @ here:

rememberableframe

the library at: java-utils

the piece of code might interest you:

    public void savesize() {             preferences preferences = preferences.usernodeforpackage(this.getclass());             preferences.put(getid() + x_key, string.valueof(getlocation().x));             preferences.put(getid() + y_key, string.valueof(getlocation().y));             preferences.put(getid() + w_key, string.valueof(getsize().width));             preferences.put(getid() + h_key, string.valueof(getsize().height));             preferences.put(getid() + max_key,                             string.valueof((getextendedstate() & jframe.maximized_both) == jframe.maximized_both));             try {                     preferences.flush();             }             catch(backingstoreexception e) {                     e.printstacktrace();             }     }      public void setsavedsize() {             preferences preferences = preferences.usernodeforpackage(this.getclass());             string xs = preferences.get(getid() + x_key, "");             string ys = preferences.get(getid() + y_key, "");             string ws = preferences.get(getid() + w_key, "");             string hs = preferences.get(getid() + h_key, "");             string max = preferences.get(getid() + max_key, "");              if(max != null && !max.trim().isempty() && boolean.valueof(max) == true) {                     setdefaultsize();                     setextendedstate(jframe.maximized_both);                     return;             }              if(xs.length() == 0 || ys.length() == 0 || ws.length() == 0 || hs.length() == 0) {                     setdefaultsize();             }             else {                     sizefrompreferences = true;                     int x = integer.parseint(xs);                     int y = integer.parseint(ys);                     int w = integer.parseint(ws);                     int h = integer.parseint(hs);                     setlocation(x, y);                     setsize(w, h);             }     } 

edit

if want create kind of settings storing system can use same convention in rememberableframe - use prefixes.

in rememberableframe use:

    private string getid() {             return this.getclass().getsimplename() + id;     } 

where id custom string provided developer or empty string. remember key of property want set has length limit.

from api:

static int  max_key_length        maximum length of string allowed key (80 characters). static int  max_name_length        maximum length of node name (80 characters). static int  max_value_length        maximum length of string allowed value (8192 characters). 

you might consider using classes "storing settings" purposes. example class keyboardsettings , in different packages.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -