Java中配置文件实例主要有两种方式,一种是通过xml配置文件,另一种是通过properties配置文件,本文主要介绍一下这两种文件加载读取方法。

一、xml配置文件

<?xmlversion="1.0"encoding="UTF-8"?>
<!-- const.xml -->
<config>
    <database>
        <url>127.0.0.1</url>
        <port>1521</port>
        <login>admin</login>
        <password>pass</password>
    </database>
</config>

1、Apache Commons Configuration读取xml配置文件

1)使用Maven引入Commons Configuration,pom.xml配置文件如下,

<dependencies>
    <dependency>
        <groupId>commons-configuration</groupId>
        <artifactId>commons-configuration</artifactId>
        <version>1.8</version>
    </dependency>
    <dependency>
        <groupId>commons-beanutils</groupId>
        <artifactId>commons-beanutils</artifactId>
        <version>1.8.0</version>
    </dependency>
    <dependency>
        <groupId>commons-jxpath</groupId>
        <artifactId>commons-jxpath</artifactId>
        <version>1.3</version>
    </dependency>
</dependencies>

2)读取xml配置文件代码

XMLConfiguration config =new XMLConfiguration("const.xml");
//127.0.0.1
config.getString("database.url"); 
//1521
config.getString("database.port");

3)通过XPath表达式读取xml配置文件代码

XMLConfiguration config =new XMLConfiguration("const.xml");
config.setExpressionEngine(new XPathExpressionEngine());
//127.0.0.1
config.getString("databases/database[name = 'dev']/url");       
//192.23.44.100
config.getString("databases/database[name = 'production']/url");

文档:http://commons.apache.org/configuration/

2、JDOM读取xml配置文件

文档:http://www.jdom.org/downloads/docs.html

SAXBuilder parser = new SAXBuilder();
Document docConfig = parser.build("config.xml");
Element elConfig = docConfig.getRootElement();
String host = elConfig.getChildText("url");

二、properties配置文件

dbpassword=password
database=localhost
dbuser=levi

1)读取properties配置文件代码如下,

Properties prop = new Properties();
InputStream input = null;

try {

	input = new FileInputStream("config.properties");

	//加载properties文件
	prop.load(input);

	//get the property value and print it out
	System.out.println(prop.getProperty("database"));
	System.out.println(prop.getProperty("dbuser"));
	System.out.println(prop.getProperty("dbpassword"));

} catch (IOException ex) {
	ex.printStackTrace();
} finally {
	if (input != null) {
		try {
			input.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

2)从项目的classpath中读取properties配置文件

Properties prop = new Properties();
InputStream input = null;
try {
    String filename = "config.properties";
    input = test.class.getClassLoader().getResourceAsStream(filename);
    if (input == null) {
        System.out.println("Sorry, unable to find " + filename);
        return;
    }
    //从class文件路径加载properties文件
    prop.load(input);
    //获取属性值并打印出来
    System.out.println(prop.getProperty("database"));
    System.out.println(prop.getProperty("dbuser"));
    System.out.println(prop.getProperty("dbpassword"));
} catch (IOException ex) {
    ex.printStackTrace();
} finally {
    if (input != null) {
        try {
            input.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

推荐文档