Java Preferences

Java中的Preferences的基本使用方法:

Java代码 复制代码 收藏代码
  1. import java.util.prefs.Preferences;
  2. public class PreferencesDemo {
  3. public static void main(String[] args) throws Exception {
  4. Preferences prefs = Preferences.userNodeForPackage(PreferencesDemo.class);
  5. prefs.put("Location", "Oz");
  6. prefs.put("Footwear", "Ruby Slippers");
  7. prefs.putInt("Companions", 4);
  8. prefs.putBoolean("Are there witches?", true);
  9. int usageCount = prefs.getInt("UsageCount", 0);
  10. usageCount++;
  11. prefs.putInt("UsageCount", usageCount);
  12. for (String key : prefs.keys()) {
  13. System.out.println(key + ": " + prefs.get(key, null));
  14. }
  15. // You must always provide a default value:
  16. System.out.println("How many companions does Dorothy have? " + prefs.getInt("Companions", 0));
  17. }
  18. }

代码来自:《Thinking in Java》

详细使用,请参考对应的JDK文档!=^_^=

posted on 2013-02-16 19:30  蜜雪薇琪  阅读(422)  评论(0编辑  收藏  举报