使用common-configuration进行对配置文件的操作(xml,properties
apache提供了commons-configuration-1.0.jar进行配置文件的操作。 可以在官网上下在common-configruation的jar包。 官方介绍:
The Commons Configuration software library provides a generic configuration interface which enables a Java application to read configuration data from a variety of sources. Commons Configuration provides typed access to single,and multi-valued configuration parameters as demonstrated by the following code: Double double = config.getDouble("number"); Integer integer getInteger Configuration parameters may be loaded from the following sources:
<?xml version="1.0" encoding="UTF-8"?> <pserson> <username> <zhangsan> 11100141 </zhangsan> <lisi> 11100142 </lisi> </username> <password>123456</password> <sex>male</sex> <address>beijingshichaoyangqu</address> </pserson> public class CommonConfigurationTest { public static void main(String[] args) { String resources = "commonConfigruationXML.xml"; try { Configuration xml = new XMLConfiguration(resources); List list =xml.getList("username.zhangsan"); System.out.println(list); } catch (ConfigurationException e) { e.printStackTrace(); } } } 对属性文件进行的操作:
dbcp.username=root dbcp.password=root driver=com.mysql.jdbc.Driver package com.jd.list.utils; import org.apache.commons.configuration.Configuration; import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configuration.PropertiesConfiguration; /** * Created by guojiangjiang on 2015/8/24. */ public class CommonConfigurationTest { public static void main(String[] args) { String resources = "commonConfigurationprop.properties"; try { Configuration prop = new PropertiesConfiguration(resources); Object object =prop.getProperty("driver"); System.out.println(object); } catch (ConfigurationException e) { e.printStackTrace(); } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |