Datasheet

for(String s : p.keys()) {
System.out.println(“ “ + s + “ = “ + p.get(s, “”));
}
}
public void setSomeProperties(Preferences p)
throws BackingStoreException
{
p.put(“fruit”, “apple”);
p.put(“cost”, “1.01”);
p.put(“store”, “safeway”);
}
public void exportToFile(Preferences p, String fileName)
throws BackingStoreException
{
try {
FileOutputStream fos = new FileOutputStream(fileName);
p.exportSubtree(fos);
fos.close();
} catch(IOException ioe) {
System.out.println(“IOException in exportToFile\n” + ioe);
ioe.printStackTrace();
}
}
public static void main(String args[])
{
PreferenceExample pe = new PreferenceExample();
Preferences prefsRoot = Preferences.userRoot();
Preferences myPrefs = prefsRoot.node(“PreferenceExample”);
try {
pe.setSomeProperties(myPrefs);
pe.printInformation(myPrefs);
pe.exportToFile(myPrefs, “prefs.xml”);
} catch(BackingStoreException bse) {
System.out.println(“Problem with accessing the backing store\n” + bse);
bse.printStackTrace();
}
}
}
The output to the screen is shown here:
Node’s absolute path: /PreferenceExample
Node’s children:
Node’s keys: fruit cost store
Node’s name: PreferenceExample
Node’s parent: User Preference Node: /
NODE: User Preference Node: /PreferenceExample
76
Part I: Thinking Like a Java Developer
05_777106 ch01.qxp 11/28/06 10:43 PM Page 76