Commons configuration

本文介绍如何利用commons-configuration库来高效管理不同类型的配置文件,包括properties与XML格式,并展示了具体的读写操作实例。

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

必须包:
commons-configuration-1.5.jar
commons-lang-2.3.jar
commons-collections-3.2.jar
commons-logging-1.1.1.jar
作用:1)提供统一管理各种配置文件的读写API
2)简化了读写流程
3)提供获取数据基本类型自动强转
基本使用:
操作properties文件
//读取
Configuration configuration = new PropertiesConfiguration("config.properties");
String name = configuration.getString("config.name");
int age = configuration.getInt("config.age");
//获取逗号分割的列表
String[] fireds = configuration.getStringArray("config.firends"); //config.firends = liguang,xuwenjian用逗号分割
//保存
PropertiesConfiguration configuration = new PropertiesConfiguration("config.properties"); //要使用类PropertiesConfiguration
configuration.setProperty("config.city", "nanjing");
configuration.save();
//保存到指定文件中
configuration.save("configbak.properties");
操作xml文件
<?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>
</buttons>
<numberFormat pattern="###\,###.##"/>
</gui-definition>

XMLConfiguration config = new XMLConfiguration("tables.xml");
//获取节点值 路径中不包括根节点名
String backColor = config.getString("colors.background");
//获取节点属性值
String linkNormal = config.getString("colors.link[@normal]");
//获取列表
List buttons = config.getList("buttons.name");
//获取逗号分割的列表
String[] fireds = config.getStringArray("config.firends"); //config.firends = liguang,xuwenjian用逗号分割
//tables.table(1).fields.field(2).name 从0开始计数
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值