Java平台 利用Common Configuration 读取配置文件

开发环境Eclipse 4.4,JDK 1.7

Apache commons-configuration 是一个开源组件,可以方便的对项目中的配置文件进行读取和保存,本文只说明如何使用该组件读取项目中的配置文件(以ini、properties、xml文件为例),保存另文描述。

首先,需要添加相关的jar,我使用的是1.7版本,利用maven导入jar包:

<dependency>
            <groupId>commons-configuration</groupId>
            <artifactId>commons-configuration</artifactId>
            <version>1.7</version>
</dependency>

除了commons-configuration.jar以外,maven会自动添加其依赖的相关jar,如commons-collections、commons-lang等。

注意,如果要使用XML作为项目配置文件,并且需要使用XPath对配置文件进行查询的话,还需要添加commons-jxpath组件,利用maven导入jar包:

<dependency>
            <groupId>commons-jxpath</groupId>
            <artifactId>commons-jxpath</artifactId>
            <version>1.3</version>
</dependency>

在src/main/resources下新建一个类,命名为ConfigApp;

在src/main/resources下新建一个config文件夹,在config文件夹中新建三个配置文件:

cfg .ini (ini文件的简述,百度百科:baike.baidu.com/view/509647.htm)

[os]
edition = windows7

cfg.properites

platform.jre = 1.7

cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<college>
	<student name="foo" gender="M">
		<score course="Algorithm">97</score>
		<score course="Operating System">97</score>
	</student>
	<student name="bar" gender="F">
		<score course="Algorithm">86</score>
		<score course="Operating System">91</score>
	</student>
        <teacher>
                <name>tee</name>
                <age>31</age>
        </teacher>
</college>

最终,项目目录应该是这个样子:

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值