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:
- Properties files
- XML documents
- Windows INI files
- Property list files (plist)
- JNDI
- JDBC Datasource
- System properties
- Applet parameters
- Servlet parameters
<?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(); } } }