必须包:
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开始计数
读取配置文件
最新推荐文章于 2024-10-09 01:46:25 发布