Source: http://blog.9cbs.net/chensheng913/archive/2004/08/25/84736.aspx
We often need to record settings in our program, such as window location, open files, user option settings, and other data to make users the next open program to continue to use this. Before our usual practice is to use the Properties class, which provides the following methods: void load (InputStream inStream) void store (OutputStream out, String header) String getProperty (String key, String defaultValue) String getProperty (String key) These methods allow us very Easy access to setting data. Another way is to use the ResourceBundle class to store setting data, and even some of the program authors use a custom structure file to store the setting data. But no matter what, the most headache is the headache: where should I save this data? Now, JDK1.4 provides us with a Java.util.prefs package, there is a Preferences class that allows the above work to be extremely easy! People who write VB procedures often use SaveSetting functions and getSettging to access registry. User setting data. The Java's Preferences class also provides a similar mechanism. The Preferences class has different implementations in different platforms. In the Windows platform, Preferences is saved in the registry in the registry, and I don't know anything in other platforms (I don't know much about Linux and other systems). But no matter what, all through the same The interface is used, and the program author can achieve details regardless of the implementation.
Establish a Preferences object
In order to distinguish the parameters of different applications, you want to specify a node path when establishing Preferences. Preferences is an abstract class that provides a series of static methods and abstract methods to operate parameters: Abstract method: preferences.UserNodeForPackage (this); preferences sysdata = preferences.systemnodeforpackage (this); these two methods are from the specified method Package returns a node path, such as this is javax.swing.jcomponent, return / javax / swing static method: preferences userdata = preferences.userroot (). Node ("/ com / sunway / SPC" ); Preferences sysdata = preferences.systemroot (). Node ("/ com / sunway / spc"); two sets of operation methods are provided for each way. One of the sets is the user parameters, and the other is the system parameter item. In the Windows platform, the root node in the registration table is the hkey_current_user / software / javasoft / prefs system parameter item in the registry is hkey_local_machine / software / javasoft / prefs and our designated node path is located in these The root node is below.
How to read and write data
Preferences provides a variety of data read and write methods. First to see the method of writing (PUT) PutBoolean (String Key, Boolean Value) PutByteaRray (String Key, Byte Value "Putdouble (String Key, String Key, Float) Putfloat Value) Putint (String Key, INT Value) below is a method of reading (GET), GET (String Key, Boolean Default) getByteaRray (String Key, Byte Default []) getfloat (String Key, Float Default) Get (String Key, Int Default) Getlong (String Key, Long Default) Note that each of the second parameters of each GET requires us Specify the default parameters for it. In addition to the above methods, Preferences also allows us to export its data to an XML file save, Void ExportNode (OutputStream OS) Void ExportSubtree (OutputStream OS) We can export a node, or export the entire child node tree .Preferences Demonstration The above explanation can write an example to see how Preferences works for us, see the following example: import java.io. *; Import java.util.prefs. *; Public class prefsdemo {public static void main (String args []) {string keys [] = {"sunway", "copyright", "author"}; string value [] = {"Sunway technology company", "Copyright 2002", "turbochen@163.com" }; / * Create a / new / sunway / SPC node parameter item under User root * / preferences prefsdemo = preferences.userroot (). Node ("/ com / sunway / spc"); / * Save Parameter * /
For (int i = 0; i Try {fileOutputStream Fos = New FileOutputStream ("prefsdemo.xml"); prefSDemo.ExportNode (fos);} catch (exception e) {system.err.println ("Cannot Export Nodes: E);} / * Remove Comments You can clear the parameter item in the registry * / / * Try {prefsdemo.removenode ();} catch (backingstoreexception e) {} * / }} The preferences feature is described above. To learn more about Preferences, please check the JDK document java.util.prefs.Preferences