Java Notes-12(Preferences API, The Logging API)

本文介绍Java中的Preferences API,用于存储小型信息,并通过树形结构组织;同时讲解了日志API,提供了灵活的日志记录框架,包括不同级别的日志记录及监听器的使用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Summary:Preferences API, The Logging API

- The Preferences API is like a portable version of the Windows registry, a mini-database in which you can keep small amounts of information, accessible to all applications

-Preferences are stored logically in a tree. 

[java]  view plain copy
  1. Preferences prefs = Preferences.userRoot().node("oreilly/learningjava");  
  2. prefs.put("author""Niemeyer");  
  3. prefs.putInt("edition"4);  
  4. String author = prefs.get("author""unknown");  
  5. int edition = prefs.getInt("edition", -1);  

-Preferences are stored in two separate trees: system preferences and user preferences.System preferences are shared by all users of the Java installation. But user preferences
are maintained separately for each user; 

-The  node() method accepts either a relative or an absolute path. 

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. Preferences prefs =  
  2. Preferences.userRoot().node("oreilly").node("learningjava");  

-Often your application should be notified if changes are made to the preferences while it’s running. You can get updates on preference changes using the  PreferenceChange
Listener and  NodeChangeListener interfaces. 

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. Preferences prefs =  
  2. Preferences.userRoot().node("/oreilly/learningjava");  
  3. prefs.addPreferenceChangeListener( new PreferenceChangeListener() {  
  4. public void preferenceChange(PreferenceChangeEvent e) {  
  5. System.out.println("Value: " + e.getKey()  
  6. " changed to "+ e.getNewValue() );  
  7. }  
  8. } );  

-The  java.util.logging package provides a highly flexible and easy-to-use logging framework for system information, error messages, and fine-grained tracing (debugging) output.

 -The heart of the logging framework is the logger, an instance of  java.util.logging.Logger . 

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.oreilly.learnjava;  
  2. public class Book {  
  3. static Logger log = Logger.getLogger("com.oreilly.learnjava.Book");  


-The logger provides a wide range of methods to log messages; some take very detailed information, and some convenience methods take only a string for ease of use

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. log.warning("Disk 90% full.");  
  2. log.info("New user joined chat room.");  

-a logger for the name “global” is provided in the static field  Logger.global . You can use it as an alternative to the old standby  System.out.println() 

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. Logger.global.info("Doing foo...")  

-Before a logger hands off a message to its handlers or its parent’s handlers, it first checks whether the logging level is sufficient to proceed. If the message doesn’t meet the required level, it is discarded at the source. 

Level                         Meaning
SEVEREApplication failure
WARNINNotification of potential problem
INFO Messages of general interest to end users
CONFIGDetailed system configuration information for administrators
FINESuccessively more detailed application tracing information for developers
FINER 
FINEST 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值