Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 244
  • Last Modified:

JDK 1.4 Preferences API

Upon reading the new Preferences API in JDK 1.4, came across that :

To get the user preferences, we would use:

Preferences prefs = Preferences.userRoot().node("/com/foo/myApp");

To get the system preferences, we would use:

Preferences prefs = Preferences.systemRoot().node("/com/foo/myApp");

What is package name doing here ???
In the JDK1.3 era of Properties files, we used to pass the name of the properties file...etc...
How do we tell the Preferences Object here, where the hell is my properties file ???

  • 2
1 Solution
From : http://java.sun.com/j2se/1.4/docs/guide/lang/preferences.html

   1. How does this Preferences API relate to Properties API?

      It is intended to replace most common uses of Properties, rectifying many of its deficiencies, while retaining its light weight. When using Properties, the programmer must explicitly specify a pathname for each properties file, but there is no standard location or naming convention. Properties files are "brittle", as they are hand-editable but easily corrupted by careless editing. Support for non-string data types in properties is non-existent. Properties cannot easily be used with a persistence mechanism other than the file system. In sum, the Properties facility does not scale.

JituAuthor Commented:
girionis >
I read the article, i still have following questions :

1. Which 'user' is being referred to here ?
Preferences prefs = Preferences.userRoot().node("/com/foo/myApp");

Is this OS user ? In case of Windows NT, the logged in user in the Windows machine ??

That would occur as a strange thing to me, because, my application would usually have registered user (example in a web application), and i would be interested in their user preferences. I am confused about this.

It seems OS user is being referred here.

2. I did not understand the need of hierarchy here especially its association with packages.
Here is what i mean :

My properties file in JDK 1.3 era would look like :

oracleUserId = myUser
oraclePassswd = myPasswd
oracleTimeOut = 3
SkinToUse = StarAndStripes
PostLoginImage = welcome1.jpg

How would i use this in my code to pick up this information or even set this information, from any class in my packages. The package names as u can see are irrelevant to me in this case, the whole application uses the same properties file.

Now, what is the use of converting this into a hierarchy. Can anyone explain with a example ?

1) Under Linux/Unix it is the user that is currently logged in. Sorry, not sure about Windows.

2) If your whole application uses the same property files and there is no need for hierarchy then I'd say there is no need to use the Preferences class. Anyhow, if you still want to use these values you will have to use the put() method (for String) or the put<type> for the rest (take a look at the Preferences API for more info: http://java.sun.com/j2se/1.4/docs/api/java/util/prefs/Preferences.html). You can also look at some source code examples here: http://javaalmanac.com/egs/java.util.prefs/pkg.html

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now