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学习笔记】个人设定相关推荐

  1. java学习笔记11--Annotation

    java学习笔记11--Annotation Annotation:在JDK1.5之后增加的一个新特性,这种特性被称为元数据特性,在JDK1.5之后称为注释,即:使用注释的方式加入一些程序的信息. j ...

  2. java学习笔记13--反射机制与动态代理

    本文地址:http://www.cnblogs.com/archimedes/p/java-study-note13.html,转载请注明源地址. Java的反射机制 在Java运行时环境中,对于任意 ...

  3. 准备写java学习笔记

    准备写java学习笔记 java int 转载于:https://blog.51cto.com/cryingcloud/1975267

  4. Java学习笔记--StringTokenizer的使用

    2019独角兽企业重金招聘Python工程师标准>>> Java Tips: 使用Pattern.split替代String.split String.split方法很常用,用于切割 ...

  5. java学习笔记12--异常处理

    java学习笔记系列: java学习笔记11--集合总结 java学习笔记10--泛型总结 java学习笔记9--内部类总结 java学习笔记8--接口总结 java学习笔记7--抽象类与抽象方法 j ...

  6. Java学习笔记(十)--控制台输入输出

    输入输出 一.控制台输入 在程序运行中要获取用户的输入数据来控制程序,我们要使用到 java.util 包中的 Scanner 类.当然 Java 中还可以使用其他的输入方式,但这里主要讲解 Scan ...

  7. java学习笔记16--I/O流和文件

    本文地址:http://www.cnblogs.com/archimedes/p/java-study-note16.html,转载请注明源地址. IO(Input  Output)流 IO流用来处理 ...

  8. java学习笔记15--多线程编程基础2

    本文地址:http://www.cnblogs.com/archimedes/p/java-study-note15.html,转载请注明源地址. 线程的生命周期 1.线程的生命周期 线程从产生到消亡 ...

  9. java学习笔记14--多线程编程基础1

    本文地址:http://www.cnblogs.com/archimedes/p/java-study-note14.html,转载请注明源地址. 多线程编程基础 多进程 一个独立程序的每一次运行称为 ...

  10. java学习笔记11--集合总结

    java学习笔记系列: java学习笔记10--泛型总结 java学习笔记9--内部类总结 java学习笔记8--接口总结 java学习笔记7--抽象类与抽象方法 java学习笔记6--类的继承.Ob ...

最新文章

  1. Apache启用mod_expires模块
  2. 人工智能28个常见术语,别再说你不懂AI了!
  3. Android中GridView的实现实例
  4. C#中巧用Lambda表达式实现对象list进行截取
  5. 发布ccnet的步骤
  6. 陶哲轩实分析 命题 8.2.6 证明
  7. 手动打开和关闭windows的相关服务
  8. anaconda在win下和在mac下的安装区别
  9. 滑雪(洛谷-P1434)
  10. 操作系统的概念,功能和目标
  11. AI 帮程序员找 Bug,一键快速预测
  12. python连接sql sever_R和python连接SQL sever 数据库操作
  13. 颗粒状糖果(巧克力)包装机设计
  14. Java 之父 James Gosling 最新访谈:JIT 很好,但不适合所有语言
  15. ESP32+AMG8833+RGB屏240*320(ST7789)红外热成像
  16. 高效工程师系列(一) 如何找到一个利于自己成长的环境
  17. 互联网行业裁员潮为什么来得这么突然?
  18. sql的datetime 数据类型
  19. springboot基于微信小程序的电器商城系统的设计与实现毕业设计源码251453
  20. jt808、obd采集源码

热门文章

  1. 游戏中每日刷新实现思路浅析
  2. 15.用PHP写出显示客户端IP与服务器IP的代码,如何防止用户使用代理的情况?[添加更多详情]...
  3. hdu1316 水大数
  4. C语言-二维数组与指针
  5. 【Android 逆向】Android 进程注入工具开发 ( 总结 | 源码编译 | 逆向环境搭建使用 | 使用进程注入工具进行逆向操作 ) ★★★
  6. 【Android 插件化】插件化技术弊端 ( 恶意插件化程序的解决方向 | 常用的插件化虚拟引擎 )
  7. 【Flutter】Flutter 混合开发 ( Flutter 与 Native 通信 | 在 Flutter 端实现 BasicMessageChannel 通信 )
  8. 【Android 安全】DEX 加密 ( Application 替换 | 兼容 ContentProvider 操作 | 源码资源 )
  9. 【组合数学】组合恒等式 ( 递推 组合恒等式 | 变下项求和 组合恒等式 简单和 | 变下项求和 组合恒等式 交错和 )
  10. 设计模式-Adapter模式