package com.acme.widget;
import java.util.prefs.*;
public class Gadget {
// Preference keys for this package
private static final String NUM_ROWS = "num_rows";
private static final String NUM_COLS = "num_cols";
private static Preferences prefs =
Preferences.userNodeForPackage(Gadget.class);
public void foo() {
int numRows = prefs.getInt(NUM_ROWS, 40);
int numCols = prefs.getInt(NUM_COLS, 80);
...
}
}
Note that static final fields are used for the key names ( NUM_ROWS and NUM_COLS) in place of inline String literals). This reduces the likelihood of runtime bugs from typographical errors in key names. Note also that reasonable defaults are provided for each of the preference values obtained. These defaults will be returned if no preference value has been set, or if the backing store is inaccessible. Note additionally that a single Preferences instance is obtained and stored in a static variable: it is perfectly acceptable for multiple threads to use a single Preferences instance concurrently with no external synchronization. Note finally that the above code accesses per-user preferences. If a single per-system value were desired, the first line in foo would be replaced by:
private static Preferences sysPrefs =
Preferences.systemNodeForPackage(Gadget.class);
The use of system preferences should be rare compared to user preferences.
We anticipate that they will be used chiefly to store configuration
information (such as what version of a program is installed or what
resources it uses). In typical Java platform implementations, ordinary users
will not be able to modify system preferences as backing stores typically
demand administrative privileges to modify system-wide data.
int x, y;
...
prefs.put(POSITION, x + "," + y);
When such a "compound preference" is read, it must be decoded. For
robustness, allowances should be made for a corrupt (unparseable) value:
static int X_DEFAULT = 50, Y_DEFAULT = 25;
void baz() {
String position = prefs.get(POSITION, X_DEFAULT + "," + Y_DEFAULT);
int x, y;
try {
int i = position.indexOf(',');
x = Integer.parseInt(position.substring(0, i));
y = Integer.parseInt(position.substring(i + 1));
} catch(Exception e) {
// Value was corrupt, just use defaults
x = X_DEFAULT;
y = Y_DEFAULT;
}
...
}
import java.util.prefs.*;
import java.io.IOException;
public class SavePrefs {
public static void main(String[] args)
throws IOException, BackingStoreException {
Preferences root = Preferences.userRoot();
root.exportSubtree(System.out);
}
}
Running this program with the following command line produces a backup of user
preferences:
java SavePrefs > backup.prefsThe following simple program imports preferences from the standard input stream:
import java.util.prefs.*;
import java.io.IOException;
public class RestorePrefs {
public static void main(String[] args)
throws IOException, InvalidPreferencesFormatException {
Preferences.importPreferences(System.in);
}
}
Running this program with the following command line restores the user
preferences from the file produced by the previous command line:
java RestorePrefs < backup.prefsIf it is run by a user other than the one that saved the preferences in the first place, it copies the preferences from that user to the current user.
private static final String BACKING_STORE_AVAIL = "BackingStoreAvail";
private static Preferences prefs =
Preferences.userNodeForPackage(ThisClass.class);
private static boolean backingStoreAvailable() {
try {
boolean oldValue = prefs.getBoolean(BACKING_STORE_AVAIL, false);
prefs.putBoolean(BACKING_STORE_AVAIL, !oldValue);
prefs.flush();
} catch(BackingStoreException e) {
return false;
}
return true;
}
Copyright 2000 Sun Microsystems, Inc., 901 San Antonio Road, Palo Alto, California 94303 U.S.A. All rights reserved.