The Java.util.prefs package, added to Java 1.4, enables you to manipulate user preference data and configuration data by providing access to the implementation-specific registry (for example, the Windows registry on the Windows platform).
Did you ever need to save your program's configuration data without knowing where to store the data? Although you can use a property file or a resource bundle to get this information, the Java platform has never specified a standard location for storing these files. After JSR 10, everything changed, and it provides an added java.util.prefs package for the Java 1.4 API. The storage mechanism is specific to the implementation details, but the programmer doesn't have to know or worry about it. For the Windows platform, its location is in the Windows registry. Although you cannot control the registry freely, you do have access to all applications through a common root node.
Begin
Naming the very appropriate Preferences class provides the basic framework for manipulating preferences. This class provides a series of static and abstract methods for manipulating two sets of preferences, one for user preferences and one for System preferences. Using static methods, you get a platform-specific implementation, just like the Windowspreferences class, and then you can use the abstract method that is implemented by this platform-specific implementation to do this work.
It's a good practice to group your program's preferences by using a package to avoid naming conflicts with other applications. When you look up a Preferences object, simply pass the name of the package. When you use a Non-static method, you can pass a reference to itself (this), and the program will determine which package you are looking for, as shown in Listing 1.
Listing 1. To obtain a Preferences object from a Non-static method
Preferences userprefs = Preferences.usernodeforpackage (this);
Preferences sysprefs = Preferences.systemnodeforpackage (this);
However, if you are using a static method, you must get the root node and provide the package yourself, as shown in Listing 2.
Listing 2. To get the Preferences object from a static method
Preferences userprefs = Preferences.userroot (). Node ("/NET/ZUKOWSKI/IBM");
Preferences sysprefs = Preferences.systemroot (). Node ("/NET/ZUKOWSKI/IBM");
Once you have the node that you are working on, you can easily set, get, drop, and dump setup options. Just treat the Preferences object as a large key-value hash list (this table organizes the keys in the tree structure). It is not part of the collection Framework (collections framework) (see Resources for more information on the collection framework).
Write Data
We'll start by discussing how to store the preferences. The Preferences class provides a series of put () methods, as shown below, for storing values. In addition to supporting basic strings, you can also store Boolean, double-precision, floating-point, integer, long, and byte arrays (consider serialization). The helper method takes the appropriate data type and performs the necessary transformations to store the data as a string.
Put (string key, String value)
Putboolean (String key, Boolean value)
Putbytearray (String key, byte value[])
Putdouble (String key, double value)
Putfloat (String key, float value)
Putint (String key, int value)
Putlong (String key, Long value)
All put () methods return a void. If the storage mechanism is not available, a backingstoreexception is thrown.
Note: The key length of a particular preference is limited to the Preferences.max_key_length (80) character, and its value is limited to Preferences.max_value_length (8192) characters.