【JAVA学习笔记】个人设定
生活随笔
收集整理的這篇文章主要介紹了
【JAVA学习笔记】个人设定
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1.簡介:?
以前你想讓程序記住用戶自定義的習慣,比如界面字體等,你使用一個配置文件,但是在維護多個用戶或出現誤拼寫時還是力不從心。而java.util.prefs包則提供了便利。在線文檔寫的非常糟糕,將java.util.prefs這個類描述為“a node in a hierarchical collection of preference data”,還說“there are two separate trees of preference nodes, one for user preferences and one for system preferences.”?
這個其實沒有那么抽象。?
2.使用:?
創建Preferences對象的方法是使用靜態方法userNodeForPackage()。這個方法要求有一個類對象(通過getClass()方法得到)作為它唯一的參數,系統以便確定某個類是駐留在某個包上的,它返回Preferences對象,這個對象可以從那個包中返回用戶設定的偏好信息。由于所有程序一般都使用它們自己的包名,這樣references對象間就不會沖突了。userNodeForPackage()方法對每個用戶返回不同的Preferences對象,因此同一個程序的不同用戶也不會沖突。?
偏好信息是以鍵值對的形式存儲的,存儲偏好設定使用put()方法,取出偏好設定使用get()方法,使用時需要有一個默認值提供。針對int的putInt方法也可以使用。相似的,?
??? * boolean - putBoolean() and getBoolean()?
??? * long - putLong() and getLong()?
??? * float - putFloat() and getFloat()?
??? * double - putDouble() and getDouble())?
clear()則清除這個鍵值對。?
3.舉例:?
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 with?
? * 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".equalsIgnoreCase (param2);?
??? }?
??? else if (operation == PUT) {?
??????? export = "export".equalsIgnoreCase (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?
下邊這篇文字更為詳細的介紹了個人偏好設定這個話題:?
http://java.sun.com/developer/technicalArticles/releases/preferences/
本文轉自gnuhpc博客園博客,原文鏈接:http://www.cnblogs.com/gnuhpc/archive/2012/12/17/2822272.html,如需轉載請自行聯系原作者
以前你想讓程序記住用戶自定義的習慣,比如界面字體等,你使用一個配置文件,但是在維護多個用戶或出現誤拼寫時還是力不從心。而java.util.prefs包則提供了便利。在線文檔寫的非常糟糕,將java.util.prefs這個類描述為“a node in a hierarchical collection of preference data”,還說“there are two separate trees of preference nodes, one for user preferences and one for system preferences.”?
這個其實沒有那么抽象。?
2.使用:?
創建Preferences對象的方法是使用靜態方法userNodeForPackage()。這個方法要求有一個類對象(通過getClass()方法得到)作為它唯一的參數,系統以便確定某個類是駐留在某個包上的,它返回Preferences對象,這個對象可以從那個包中返回用戶設定的偏好信息。由于所有程序一般都使用它們自己的包名,這樣references對象間就不會沖突了。userNodeForPackage()方法對每個用戶返回不同的Preferences對象,因此同一個程序的不同用戶也不會沖突。?
偏好信息是以鍵值對的形式存儲的,存儲偏好設定使用put()方法,取出偏好設定使用get()方法,使用時需要有一個默認值提供。針對int的putInt方法也可以使用。相似的,?
??? * boolean - putBoolean() and getBoolean()?
??? * long - putLong() and getLong()?
??? * float - putFloat() and getFloat()?
??? * double - putDouble() and getDouble())?
clear()則清除這個鍵值對。?
3.舉例:?
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 with?
? * 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".equalsIgnoreCase (param2);?
??? }?
??? else if (operation == PUT) {?
??????? export = "export".equalsIgnoreCase (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?
下邊這篇文字更為詳細的介紹了個人偏好設定這個話題:?
http://java.sun.com/developer/technicalArticles/releases/preferences/
本文轉自gnuhpc博客園博客,原文鏈接:http://www.cnblogs.com/gnuhpc/archive/2012/12/17/2822272.html,如需轉載請自行聯系原作者
總結
以上是生活随笔為你收集整理的【JAVA学习笔记】个人设定的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Ubuntu文件管理点击没有反应
- 下一篇: Unable to open file