13

Say I have the following

Preferences prefs = Preferences.userRoot().node(this.getClass().getName());

String ID1 = "Test1";

System.out.println(prefs.getBoolean(ID1, true));

prefs.putBoolean(ID1, false);

//prefs.remove(ID1);
  1. Is this variable persistent the next time I execute my program?
  2. Where do these variables store?
  3. What is the proper way of utilizing this?
  4. Is the approach better than using properties files?
stackoverflow
  • 18,348
  • 50
  • 129
  • 196
  • 3
    Or from a version that wasn't EOLd years ago: http://docs.oracle.com/javase/6/docs/api/java/util/prefs/Preferences.html – Dave Newton Apr 20 '12 at 13:39

1 Answers1

19
  1. Yes, the value is persistent but only for the user. It won't be there for other users.
  2. This is OS specific. For Windows it uses the registry, for Linux I believe it uses hidden files in the user root, although I'm not 100% sure.
  3. You've got a pretty good example in your question.
  4. It is different, not better. Preferences are a way of transparently storing settings for an application. These settings may be updated in run time by a user (for example you could use prefs to store user specific settings). Preferences are not meant to be editable outside of the application. Properties files tend to store hard setting specific to an application. These settings are the same for each user and tend not to change often. Properties files are text files and tend to accompany an application on deployment. You can edit them easily using a text editor. It is fairly rare for an application to update properties files.
Qwerky
  • 18,217
  • 6
  • 44
  • 80
  • 1
    Note that we're talking OS users here... so an app running under user "tomcat" servers the same 'user' preference to all web users of that application. – Geert Schuring Oct 17 '13 at 12:12
  • 4
    For #1, this value is for the user because of `.userRoot()`. To save it for all users, use `.systemRoot()` – Ron Nov 16 '13 at 10:28