【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 API 微服务
2025 年 Java 从入门到精通学习笔记全新版
《Java学习笔记:从入门到精通(2025更新版)》是一本全面覆盖Java开发核心技能的指南,适合零基础到高级开发者。内容包括Java基础(如开发环境配置、核心语法增强)、面向对象编程(密封类、接口增强)、进阶技术(虚拟线程、结构化并发、向量API)、实用类库与框架(HTTP客户端、Spring Boot)、微服务与云原生(容器化、Kubernetes)、响应式编程(Reactor、WebFlux)、函数式编程(Stream API)、测试技术(JUnit 5、Mockito)、数据持久化(JPA、R2DBC)以及实战项目(Todo应用)。
692 5
|
存储 Java
# 【Java全栈学习笔记-U1-day02】变量+数据类型+运算符
本篇笔记主要围绕Java全栈学习的第二天内容展开,涵盖了变量、数据类型、运算符以及Scanner类的应用。首先介绍了变量的概念与命名规范,以及如何定义和使用变量;接着详细讲解了Java中的基本数据类型,包括整型、浮点型、字符型、布尔型等,并通过实例演示了数据类型的运用。随后,深入探讨了各类运算符(赋值、算术、关系、逻辑)及其优先级,帮助理解表达式的构成。最后,介绍了如何利用Scanner类实现用户输入功能,并通过多个综合示例(如计算圆面积、购物打折、变量交换及银行利息计算)巩固所学知识。完成相关作业将进一步加深对这些基础概念的理解与实践能力。
379 13
|
小程序 Java 知识图谱
Java 学习笔记 —— BMI & BMR 计算器
这是一个使用 Java 编写的 BMI 与 BMR 计算器小程序,可输入年龄、性别、身高和体重,计算身体质量指数(BMI)和基础代谢率(BMR),并输出健康评估结果。通过该项目,掌握了 Java 的输入处理、数据验证、条件判断、数学运算及格式化输出等基础知识,是 Java 初学者的理想练习项目。
Java 数组学习笔记
本文整理Java数组常用操作:遍历、求和、查找、最值及二维数组行求和等典型练习,涵盖静态初始化、元素翻倍、去极值求平均等实例,帮助掌握数组基础与应用。
|
存储 Java
Java学习笔记 List集合的定义、集合的遍历、迭代器的使用
Java学习笔记 List集合的定义、集合的遍历、迭代器的使用
481 4
|
开发框架 Java 开发工具
【Java全栈学习笔记-U1-day01】Java介绍
本笔记整理了Java学习的基础内容,涵盖程序理解、Java语言特性、JDK安装与配置、Java程序开发工具及编写步骤。重点介绍了Java程序的基本结构、编译和运行过程,以及输出语句的使用。通过实例演示了IDEA创建Java程序的方法,并强调了编码规范和注意事项。适合初学者复习和交流学习。 主要内容: 1. 理解程序:计算机组成、程序定义。 2. 简介:Java语言特点、技术平台、JDK作用。 3. 编写Java程序:编写、编译、运行步骤,基本结构。 4. 输出语句 5. DEA使用:新建工程、保存位置、文件介绍、新建类。 6. 扩展:注释、代码规范、大小写敏感、缩进等。
|
存储 安全 Java
Java修仙之路,十万字吐血整理全网最完整Java学习笔记(基础篇)
从Java环境的搭建到实际代码的编写,从基本用法的讲解到底层原理的剖析,深度解析Java基础知识。本文是《Java学习路线》专栏的起始文章,旨在提供一套完整的Java学习路线,覆盖Java基础知识、数据库、SSM/SpringBoot等框架、Redis/MQ等中间件、设计模式、架构设计、性能调优、源码解读、核心面试题等全面的知识点,并在未来不断更新和完善,帮助Java从业者在更短的时间内成长为高级开发。
Java修仙之路,十万字吐血整理全网最完整Java学习笔记(基础篇)
|
存储 安全 Java
Java修仙之路,十万字吐血整理全网最完整Java学习笔记(进阶篇)
本文是Java基础的进阶篇,对异常、集合、泛型、Java8新特性、I/O流等知识进行深入浅出的介绍,并附有对应的代码示例,重要的地方带有对性能、底层原理、源码的剖析。适合Java初学者。
Java修仙之路,十万字吐血整理全网最完整Java学习笔记(进阶篇)
|
存储 安全 Java
Java修仙之路,十万字吐血整理全网最完整Java学习笔记(高级篇)
本文是“Java学习路线”中Java基础知识的高级篇,主要对多线程和反射进行了深入浅出的介绍,在多线程部分,详细介绍了线程的概念、生命周期、多线程的线程安全、线程通信、线程同步,并对synchronized和Lock锁;反射部分对反射的特性、功能、优缺点、适用场景等进行了介绍。
Java修仙之路,十万字吐血整理全网最完整Java学习笔记(高级篇)
|
SQL druid Java
Java数据库部分(MySQL+JDBC)(二、JDBC超详细学习笔记)(下)
Java数据库部分(MySQL+JDBC)(二、JDBC超详细学习笔记)
399 3
Java数据库部分(MySQL+JDBC)(二、JDBC超详细学习笔记)(下)