[Java Study Notes] Personal settings

Source: Internet
Author: User

Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/

1. Introduction:
In the past, you wanted the program to remember the User-Defined habits, such as the UI font. You used a configuration file, but it still failed to maintain multiple users or make spelling mistakes. The Java. util. prefs package provides convenience. The online documentation is very poorly written. util. the prefs class is described as "a node in a hierarchical collection of preference Data". It also says "there are two separate trees of preference nodes, one for user preferences and one for system preferences."
This is not so abstract.
2. Use:
To create a preferences object, use static usernodeforpackage (). This method requires a Class Object (obtained through the getclass () method) as its unique parameter. The system determines that a class resides in a certain package and returns the preferences object, this object can return User-Defined preference information from that package. Because all programs generally use their own package names, the references object will not conflict. The usernodeforpackage () method returns different preferences for each user, so different users in the same program will not conflict.
Preferences are stored as key-value pairs. The put () method is used for preferences. The get () method is used for preferences. a default value is required for preferences. The putint Method for int can also be used. Similar,
* Boolean-putboolean () and getboolean ()
* Long-putlong () and getlong ()
* Float-putfloat () and getfloat ()
* Double-putdouble () and getdouble ())
Clear () clears this key-value pair.
3. Example:
Import java. util. prefs .*;
/**
* Simple demonstration of the most common usage of the Java
* Preferences API using the user and Package Based Storage
* Node. This app uses the user tree to avoid collisions
* Other users and uses the package name, as is conventional,
* To avoid collisions with other applications in other packages.
*
* This is a simple command-line application. It stores only one
* Key/value pair, in which key is the string "prefsvalue ".
*
* Argument 1 may be either "get", "clear", or "put ".
*
* If "get", the value stored under the key "prefsvalue" is
* Fetched and displayed.
*
* If "clear", all prefs items for this package are cleared.
*
* If "put", the second command-line argument provides the value
* To be stored. If the second argument is null, a suitable default
* Value is used.
*
* If "get" is requested the first time this application is run
* Or after a "clear" operation, a suitable default value is
* Returned.
*
**/
Public class prefsdemo {
// Define constants for the three possible operations.
Private Static final int get = 1;
Private Static final int clear = 2;
Private Static final int put = 3;
/** Constructs the prefsdemo application .**/
Public prefsdemo (string [] ARGs ){
// Get the preferences node for this user and this package.
Preferences prefs = preferences. usernodeforpackage (getclass ());
// Decode the command-line arguments.
String command = NULL;
String param2 = NULL;
String param3 = NULL;
String newvalue = NULL;
Boolean export = false;
System. Err. println ("");
If (ARGs. Length = 0 ){
System. Err. println ("no command given, assuming 'get '");
Command = "get ";
}
Else if (ARGs. Length = 1 ){
Command = ARGs [0];
}
Else if (ARGs. Length = 2 ){
Command = ARGs [0];
Param2 = ARGs [1];
}
Else if (ARGs. Length = 3 ){
Command = ARGs [0];
Param2 = ARGs [1];
Param3 = ARGs [2];
}
// Turn the string commands into ints so they can be used
// In a switch.
Int operation;
If (command. Equals ("get ")){
Operation = get;
}
Else if (command. Equals ("clear ")){
Operation = clear;
}
Else if (command. Equals ("put ")){
Operation = put;
Newvalue =
Param2! = NULL? Param2: "You forgot the value, dummy ";
}
Else {
System. Err. println
("Don't understand command '" + command + "', assuming 'get '");
Operation = get;
}
// See if the 2nd parameter (for get and clear) or
// 3rd parameter (for put) is the string "Export ".
If (Operation = GET | operation = clear ){
Export = "Export". inclusignorecase (param2 );
}
Else if (Operation = put ){
Export = "Export". inclusignorecase (param3 );
}
// Do the operation requested by the command-line argument (s ).
Switch (operation ){
Case clear:
System. Err. println ("clearing Preferences ");
Try {
Prefs. Clear ();
}
Catch (backingstoreexception BSE ){
System. Err. println (BSE );
}
Break;
Case get:
String prefs_value = prefs. Get ("prefsvalue", "Default Value ");
System. Err. println
("Got prefsvalue '" + prefs_value + "' from prefs ");
Break;
Case put:
System. Err. println ("putting" + newvalue + "'into prefs ");
Prefs. Put ("prefsvalue", newvalue );
Int num_puts = prefs. getint ("num_puts", 0 );
Prefs. putint ("num_puts", num_puts + 1 );
System. Err. println
("Number of puts since clear is" + (num_puts + 1 ));
Break;
} // Switch
If (export ){
Try {
Prefs. exportnode (system. Out );
}
Catch (Java. Io. ioexception IOE ){
System. Err. println (IOE );
}
Catch (backingstoreexception BSE ){
System. Err. println (BSE );
}
}
} // Ctor
Public static void main (string [] ARGs ){
New prefsdemo (ARGs );
} // Main
} // Class prefsdemoapp
The following text details the topic of personal preferences:
Http://java.sun.com/developer/technicalArticles/releases/preferences/

 

Author: gnuhpc
Source: http://www.cnblogs.com/gnuhpc/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.