JDK 1.4 Preferences API

Posted on 2003-03-17
Medium Priority
Last Modified: 2008-02-01
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 ???

Question by:Jitu
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 35

Expert Comment

ID: 8151810
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.

LVL 35

Expert Comment

ID: 8151859

Author Comment

ID: 8157256
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 ?

LVL 35

Accepted Solution

girionis earned 150 total points
ID: 8158265
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month14 days, 9 hours left to enroll

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question