• apache commons configuration


    使用Commons  Configuration可以很好的管理我们的配置文件的读写。

    官网:http://commons.apache.org/proper/commons-configuration/

    需要使用到的包有:commons-collections、commons-configuration、commons-lang、commons-logging

    引入Maven依赖:

    <dependency>
        <groupId>commons-configuration</groupId>
        <artifactId>commons-configuration</artifactId>
        <version>1.10</version>
    </dependency>
    <dependency>
        <groupId>commons-collections</groupId>
        <artifactId>commons-collections</artifactId>
        <version>3.2.2</version>
    </dependency>
    <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <version>2.6</version>
    </dependency>
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.2</version>
    </dependency>

    一、读取xml文件

    类路径下config/message.xml内容如下:

    <?xml version="1.0" encoding="UTF-8"?>
    <config>
        <Account type="by0003">
            <code>100001</code>
            <pass>123</pass>
            <name>李四</name>
            <money>1000000.00</money>
        </Account>
    </config>

    读取配置文件:

    Configuration config = new XMLConfiguration("config/message.xml");
    String name = config.getString("Account.name");
    System.out.println("name:" + name);

    注意:config.getString(“Account.name”)中的参数是Account.name,这个参数是XPath格式的,而且不能包含xml中的根元素。

    二、读取properties文件

    config/message.properties,内容如下:

    timout=15.52  
    interactive=true  
    color=red  
    speed=50  
    name=Default Use

    读取properties文件:

    Configuration config = new PropertiesConfiguration("config/message.properties");
    String name = config.getString("name");
    System.out.println("name:" + name);

    注:URLClassLoader能查找任意目录下的文件。

  • 相关阅读:
    D. Babaei and Birthday Cake--- Codeforces Round #343 (Div. 2)
    Vijos P1389婚礼上的小杉
    AIM Tech Round (Div. 2) C. Graph and String
    HDU 5627Clarke and MST
    bzoj 3332 旧试题
    codeforces 842C Ilya And The Tree
    codesforces 671D Roads in Yusland
    Travelling
    codeforces 606C Sorting Railway Cars
    codeforces 651C Watchmen
  • 原文地址:https://www.cnblogs.com/myitnews/p/13833597.html
Copyright © 2020-2023  润新知