使用Apache commons-configuration读取配置文件
来源:互联网 发布:mysql source用法 编辑:程序博客网 时间:2024/04/20 15:15
使用Apache commons-configuration读取配置文件
Apache Commons Configuration是一个java应用程序的配置管理类库。可以从properties或者xml文件中加载软件的配置信息,用来构建支撑软件运 行的基础环境。在一些配置文件较多较的复杂的情况下,使用该配置工具比较可以简化配置文件的解析和管理。也提高了开发效率和软件的可维护性。
下面举个例子,其实Apache官网也有例子,可以前去看看。 http://commons.apache.org/proper/commons-configuration/index.html
config.properties
############ platform 平台 ################
## platform --> windows
## platform --> linux
qiuzhping.platform=1
############ key 环境变量名字 ################
qiuzhping.key=path
############存放临时文件地址 ###################
qiuzhping.tempdir=c:/paths
config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<config>
<database>
<name>dev</name>
<url>127.0.0.1</url>
<port>1521</port>
<login>root</login>
<password>123456</password>
</database>
</config>
测试程序:这两个属性文件和这个Java类处于同一文件夹内。
/** System Abbrev :* system Name :* Component No :* Component Name:* File name :TestConfiguration.java* Author :Qiuzhping* Date :2016年1月31日* Description : <description>*//* Updation record 1: * Updation date : 2016年1月31日 * Updator : Qiuzhping * Trace No: <Trace No> * Updation No: <Updation No> * Updation Content: <List all contents of updation and all methods updated.> */package com.qiuzhping.test;import org.apache.commons.configuration.CompositeConfiguration;import org.apache.commons.configuration.PropertiesConfiguration;import org.apache.commons.configuration.XMLConfiguration;/** * <Description functions in a word> * <Detail description> * * @author Qiuzhping * @version [Version NO, 2016年1月31日] * @see [Related classes/methods] * @since [product/module version] */public class TestConfiguration {/** <Description functions in a word> * * <Detail description> * @author Qiuzhping * @param args * @see [Related classes#Related methods#Related properties] */public static void main(String[] args) {try {CompositeConfiguration config = new CompositeConfiguration();config.addConfiguration(new PropertiesConfiguration(TestConfiguration.class.getResource("config.properties").getPath()));config.addConfiguration(new XMLConfiguration(TestConfiguration.class.getResource("config.xml").getPath()));System.out.println(config.getString("qiuzhping.platform"));System.out.println(config.getString("database.url"));} catch (Exception e) {e.printStackTrace();}}}输出:
1
127.0.0.1
个人感觉这个挺好用的,比原生Java的Properties扩展了很多内容。
- 使用Apache commons-configuration读取配置文件
- 配置文件读取类:commons-configuration使用
- 使用Apache Commons Configuration读取配置信息
- 使用apache commons包读取配置文件【转】
- Apache Commons Configuration 使用
- org.apache.commons.configuration.Configuration 配置文件
- 使用apache common configuration读取配置文件或修改配置文件
- 使用commons configuration管理配置文件
- [j2ee]Apache Commons Configuration 使用
- Apache Commons configuration使用入门
- Apache Commons Configuration使用入门
- Apache Commons Configuration使用入门
- Apache Commons Configuration使用入门
- Apache Commons Configuration 读取配置信息
- Apache Commons Configuration读取xml配置
- Apache 项目 之 commons-configuration-1.10.jar 使用 properties配置文件时的编码设置
- 使用commons的Configuration读取XML
- 使用commons的Configuration读取XML
- windows(32位 64位)下python安装mysqldb模块
- 无平方因子的数(数论好题)
- php5.5+apache2.4
- swift数组中添加不同类型元素
- 软件应届毕业生的2015-7到2016-2的工作总结
- 使用Apache commons-configuration读取配置文件
- cocos2d-x Button的监听使用
- 【NYOJ】[198]数数
- 欢迎使用CSDN-markdown编辑器
- 用Protocol来分离的View和Model
- mongodb 交互式操作和script文件脚本的区别。
- 安卓基础案例 电话+短信
- 【NYOJ】[199]无线网络覆盖
- Android-->Dialog/DialogFragment宽度高度修改/全屏,自定义样式