Configuration -Properties 文件

Properties文件

Properties 文件是配置程序的一种常用作法. Commons Configuration当然支持这种格式并且增强 java.util.Properties  这个类 这一节就是介绍PropertiesConfiguration  类的一些特性。

Loading 加载

At 首先,假设有个应用程序的所有配置信息都在一个叫“usergui.properties ”的properties 文件里,文件的内容如下:

# Properties definining the GUI
colors.background = #FFFFFF  

要加载这个文件,你可以像下面这样写:

Configuration config = new PropertiesConfiguration("usergui.properties");

If 如果你可以使用文件的绝对路径,那么这个文件会自动在下面几个地方被搜寻:

  • 当前目录   
  • 用户主目录
  • classpath
Includes 包含文件

If a 如果property文件里有叫做 "include "的键值对,并且值是一个 property文件名, 这么个文件也将被自动包含进配置信息中,举例如下:

# usergui.properties
include = colors.propertiesinclude = sizes.properties
# colors.properties
colors.background = #FFFFFF
Automatic Reloading 自动重新加载

A common issue with properties file is to handle the reloading of the file when it changes.

通常你需要开启一个线程来监视配置文件的时间,并在文件被修改后重新加载进来。 Commons Configuration集成了这个加载机制, 如果需要使用自动加载,只需要在年id配置信息里声明一个自动重载策略:

 

PropertiesConfiguration config = new PropertiesConfiguration("usergui.properties");
config.setReloadingStrategy(new FileChangedReloadingStrategy());                          

 

现在你随时手动修改了usergui.properties 配置信息都能够自动刷新,修改后的值立即在程序里生效。

Saving 保存

调用save()方法就可以保存你的配置:

PropertiesConfiguration config = new PropertiesConfiguration("usergui.properties");
config.setProperty("colors.background", "#000000);                                                  
config.save();                                                                                                              

你也可以复制一份配置保存到另外一个文件:

PropertiesConfiguration config = new PropertiesConfiguration("usergui.properties");
config.setProperty("colors.background", "#000000);                                                  
config.save("usergui.backup.properties);                                                                    

如果你不想在配置信息改变之后费心的手动保存文件,你可以激活自动保存模式::

PropertiesConfiguration config = new PropertiesConfiguration("usergui.properties");                
config.setAutoSave(true);                                                                                                            
config.setProperty("colors.background", "#000000); // the configuration is saved after this call
Lists and arrays

Commons Configuration 可以很轻松的返回一组值, 例如你的文件包含了用逗号分割的一组数据:

# chart colors                                           
colors.pie = #FF0000, #00FF00, #0000FF

你不用手动拆分字符串,可以直接作为数组返回:

String[] colors = config.getStringArray("colors.pie");

另外一种作法, 你可以通过多行的同名键值对,来返回一组值。

# chart colors            
colors.pie = #FF0000;
colors.pie = #00FF00;
colors.pie = #0000FF;
变量窜改

如果你熟悉Ant或者Maven,你肯定已经用到 (像 ${token} )这样可以在配置文件被加载时自动扩充的变量。 Commons Configuration 也支持这样的特性, 下面是个示例:

application.name = Killer                                                          
Appapplication.version = 1.6.2                                                
application.title = ${application.name} ${application.version}
特殊字符

如果你需要用到一些特殊字符,比如换行符,制表符,或者unicode字符,你需要进行字符转义,字符串分隔符(默认的是逗号“,”)也需要被转义  :

key = This /n string /t contains /, escaped // characters /u0020
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值