Java程序中两种配置文件(xml和properties)的加载读取方法

Java程序中,经常需要从配置文件中加载并读取设置,以支持不同的配置环境和参数。最常用的配置文件格式是XML和properties。两种方法都非常基础,适合于简单的配置文件读取需求。对于更复杂的需求,可能需要更高级的解析技术或第三方库。

参考文档:Java程序中两种配置文件(xml和properties)的加载读取方法-CJavaPy

一、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");

文档:Commons Configuration – Java Configuration API

2、JDOM读取xml配置文件

文档:JDOM: Documentation

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();
        }
    }

 参考文档:Java程序中两种配置文件(xml和properties)的加载读取方法-CJavaPy

posted @ 2024-04-10 20:44  leviliang  阅读(106)  评论(0编辑  收藏  举报