Apache Commons-configuration 学习笔记

转载自 http://wangxin0072000.iteye.com

Apache Commons-configuration 学习笔记
1 如果要使用configuration这个包,首先要保证使用JDK1.2以上,还要引入如下jar包
commons-beanutils
commons-lang
commons-logging
commons-collections
commons-digester
commons-codec
commons-jxpath
2 commons-configuration最新的版本是1.5,最主要的作用是读取资源文件,每一种文件格式都有一个对应的类,如下
properties文件--PropertiesConfiguration类
xml文件--XMLConfiguration
.ini文件--INIConfiguration
.plist文件--PropertyListConfiguration
还可以从JNDI中读取properties--JNDIConfiguration
当然还可以使用system的properties--SystemConfiguration
等等...

3 上面已经对它做了基本的介绍,下面看看这个包怎么用吧
比如我们要读一个properties文件:在classpath下面建立目录te,之后在te下面建立
test.properties

ip=127.0.0.1
port=8080
id=111
application.name = Killer App
application.version = 1.6.2
application.title = ${application.name} ${application.version}
keys=cn,com,org,uk,edu,jp,hk

[code]
//注意路径默认指向的是classpath的根目录
Configuration config = new PropertiesConfiguration("te/test.properties");
String ip=config.getString("ip");
int port=config.getInt("port");
String title=config.getString("application.title");
//再举个Configuration的比较实用的方法吧,在读取配置文件的时候有可能这个键值对应的值为空,那么在下面这个方法中
//你就可以为它设置默认值。比如下面这个例子就会在test.properties这个文件中找id的值,如果找不到就会给id设置值为123
//这样就保证了java的包装类不会返回空值。虽然功能很简单,但是很方便很实用。
Integer id=config.getInteger("id", new Integer(123));
//如果在properties 文件中有如下属性keys=cn,com,org,uk,edu,jp,hk
//可以实用下面的方式读取:
String[] keys1=config.getStringArray("keys");
List keys2=config.getList("keys");
[/code]
首先要把这个文件读到程序中,我们可以用上面的类,所有的读取文件的类都 继承自AbstractConfiguration类,
而这个类实现了Configuration接口,如果我们只有比较简单的需求,那么可以直接向上转型为Configuration类型,如果向上
转型为AbstractConfiguration类,那么可以做一些配置工作。
比如刚刚看到的 keys=cn,com,org,uk,edu,jp,hk这个例子中如果是keys=cn/com/org/uk/edu/jp/hk
那么如果还按照原来的方式读取,读到的将是"cn/com/org/uk/edu/jp/hk"这个字符串,所以要先改变分割符之后在读


AbstractConfiguration.setDefaultListDelimiter('/');
Configuration config = new PropertiesConfiguration("te/Vasp.properties");
String[] keys=config.getStringArray("keys");
List key2=config.getList("keys");

如果你看它的源码,你会在 AbstractConfiguration这个类中找到:

private static char defaultListDelimiter = ',';


如果你确定要解析的文件是properties ,那么你完全可以使用
[code]
PropertiesConfiguration config = new PropertiesConfiguration("tt/t.properties");
[/code]
代替
[code]
Configuration config = new PropertiesConfiguration("tt/t.properties");
[/code]
这么写似乎不符合面向接口编程,但是它可以使用PropertiseConfiguration中特殊的方法。
比如,你如果你想修改propertise文件,那么可以这样:
[code]
config.setProperty("id", "sss");
config.save();
[/code]
当然,你还可以使用isAutoSave这个方法:
[code]
config.isAutoSave();
config.setProperty("tttt", "sss");
[/code]
properties这个类还支持文件的继承,比如在test.properties这个文件中有
[code]
include = test1.properties
[/code]
同时在classpath中又有test1.properties这个文件,那么PropertiesConfiguration类就可以
从test1.properties文件中读取信息。

读取XML
假如有个XML,名叫:XMLtest.xml如下:
[code]
<?xml version="1.0" encoding="ISO-8859-1" ?>
<gui-definition>
<colors>
<background>#808080</background>
<text>#000000</text>
<header>#008000</header>
<link normal="#000080" visited="#800080"/>
<default>${colors.header}</default>
</colors>
<rowsPerPage>15</rowsPerPage>
<buttons>
<name>OK,Cancel,Help</name>
<name>Yes,No,Cancel</name>
</buttons>
<numberFormat pattern="###\,###.##"/>
</gui-definition>
[/code]
标准的XML文件都会有一个跟标签包裹住所有内容,这个标签的子标签认为是顶级名字空间
(这个说法可能不准确)不多说了看代码吧
[code]
XMLConfiguration config = new XMLConfiguration("XMLtest.xml");
/**
*<colors>
* <background>#808080</background>
* <text>#000000</text>
* <header>#008000</header>
* <link normal="#000080" visited="#800080"/>
* <default>${colors.header}</default>
*</colors>
*这是从上面的xml中摘抄的一段,我们现在来解析它,
*colors是根标签下的直接子标签,所以是顶级名字空间
*/
String backColor = config.getString("colors.background");
String textColor = config.getString("colors.text");
//现在我们知道了如何读取标签下的数据,那么如何读标签中的属性呢?看下面
//<link normal="#000080" visited="#800080"/>
String linkNormal = config.getString("colors.link[@normal]");
//还支持引用呢!
//<default>${colors.header}</default>
String defColor = config.getString("colors.default");
//也支持其他类型,但是一定要确定类型正确,否则要报异常哦
//<rowsPerPage>15</rowsPerPage>
int rowsPerPage = config.getInt("rowsPerPage");
[/code]
[code]
/**
*但是我们如何读取List呢
*看这里:
*<buttons>
* <name>OK,Cancel,Help</name>
* <name>Yes,No,Cancel</name>
*</buttons>
*/
这时我们可以用:
List buttons = config.getList("buttons.name");
for(String button:buttons){
System.out.println(button);
}
[/code]
但是显示的是
[code]
OK
Cancel
Help
Yes
No
Cancel
[/code]
我们想要的是
[code]
OK,Cancel,Help
Yes,No,Cancel
[/code]
如果看过之前的文章就会知道,我们有一些设置没有设
也就是我们要禁用List分隔符delimiter.在AbstractionConfiguration
这个类中(XMLConfiguration的父类)有这个setDelimiterParsingDisabled方法可以
禁用分隔符。但是要在读取XML文件之前设置这个属性。所以代码改为
[code]
XMLConfiguration config = new XMLConfiguration();
config.setDelimiterParsingDisabled(true);
config.setFileName("XMLtest.xml");
config.load();
List<String> buttons = config.getList("buttons.name");
for(String button:buttons){
System.out.println(button);
[/code]
这就可以了。


再来个更加复杂一点的XML
[code]
<?xml version="1.0" encoding="ISO-8859-1" ?>
<database>
<tables>
<table tableType="system">
<name>users</name>
<fields>
<field>
<name>uid</name>
<type>long</type>
</field>
<field>
<name>uname</name>
<type>java.lang.String</type>
</field>
<field>
<name>firstName</name>
<type>java.lang.String</type>
</field>
<field>
<name>lastName</name>
<type>java.lang.String</type>
</field>
<field>
<name>email</name>
<type>java.lang.String</type>
</field>
</fields>
</table>
<table tableType="application">
<name>documents</name>
<fields>
<field>
<name>docid</name>
<type>long</type>
</field>
<field>
<name>name</name>
<type>java.lang.String</type>
</field>
<field>
<name>creationDate</name>
<type>java.util.Date</type>
</field>
<field>
<name>authorID</name>
<type>long</type>
</field>
<field>
<name>version</name>
<type>int</type>
</field>
</fields>
</table>
</tables>
</database>
[/code]
这个XML的结构是根节点为database
[code]
database
|-table
|-name
|-fields
|-field
|-field
|-table
|-name
|-fields
|-field
|-field

[/code]
眼尖的一眼就能看穿这是一个数据库的定义文件,那么我们如何读取呢?
好吧,我们接上文,来看看吧
[code]
XMLConfiguration config = new XMLConfiguration();
config.setDelimiterParsingDisabled(true);
config.setFileName("tt/test2.xml");
config.load();
Object prop = config.getProperty("tables.table.name");
if(prop instanceof Collection)
{
System.out.println("Number of tables: " + ((Collection) prop).size());
}
[/code]
前面的我就不说了,看看这句
[code]
Object prop = config.getProperty("tables.table.name");
if(prop instanceof Collection)
{
System.out.println("Number of tables: " + ((Collection) prop).size());
}
[/code]
config读取属性的时候,会去看得到的是否是多个值,如果是,则把这些值存为一个Collection
对象。 所以会打印出
[code]
Number of tables: 2
[/code]
在使用getProperty方法时,我们给不同的节点路径会给我们不同的值,直接看例子:
[code]
//return users
System.out.println(config.getProperty("tables.table(0).name"));
//return system
System.out.println(config.getProperty("tables.table(0)[@tableType]"));
//return documents
System.out.println(config.getProperty("tables.table(1).name"));
//return null,因为只有两个table所以这个值为null
System.out.println(config.getProperty("tables.table(2).name"));
//return [docid, name, creationDate, authorID, version]
//如果所要找的节点不存在唯一值,则返回Collection类型
System.out.println(config.getProperty("tables.table(1).fields.field.name"));
//[long, long]
//与上面的相同,返回值不唯一
System.out.println(config.getProperty("tables.table.fields.field(0).type"));
//return creationDate
System.out.println(config.getProperty("tables.table(1).fields.field(2).name"));
[/code]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值