配置文件讀取--Apache Commons Configuration

關(guān)鍵字:配置文件讀取 Apache Commons Configuration

Apache Commons Configuration提供了對多種常見類型的配置文件的讀取,如對porperties、xml、ini等配置文件的讀取。

依賴的三方j(luò)ar包:
commons-configuration-1.10.jar
commons-lang-2.6.jar(configuration必須依賴的包)
commons-logging-1.2.jar(configuration必須依賴的包)

解析xml文件必須使用的依賴包:
commons-collections-3.2.2.jar
commons-jxpath-1.3.jar(采用xpath方式讀取xml配置文件需要使用到的JAR包)

Properties配置文件的讀取
(PropertiesConfiguration getString(String Key)方法)

###db.properties:配置文件內(nèi)容如下:
###common configuration start
hibernate.show_sql=true
hibernate.generate_statistics=false
hibernate.default_schema=dsportal
hibernate.hbm2ddl.auto=none
jdbc.initialSize=10
jdbc.testWhileIdle=true
jdbc.url=jdbc:postgresql://192.168.2.136:8110/dsdb
jdbc.username=DSPORTAL
jdbc.password=k0rs59bM+cYHQSZdzwQJFQ==
###common configuration end

代碼示例如下:

public class PropertiesFile {
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println(getPropertiesPara("jdbc.url", "db.properties"));
        System.out.println(getPropertiesPara("jdbc.username", "db.properties"));
    }
     
     
    public static String getPropertiesPara(String parameterName , String propertiesPath) {
        String parameterValve = null;
        try {
            File propertiesFile = new File(propertiesPath);
            PropertiesConfiguration pcf = new PropertiesConfiguration(propertiesFile);
            parameterValve = pcf.getString(parameterName);
        } catch (ConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return parameterValve;
    }
 
}

ini類型配置文件讀取
(HierarchicalINIConfiguration getString(String Key)方法)

DSNetComm.ini配置文件內(nèi)容如下:

[Cast Settings]
//Multicast port 9876 by default program
Port=10235

//Multicast uses local card, the program defaults to the first piece of card-enabled
LocalAddr=192.168.2.135

//Will join the multicast address (group), the program default 234.5.6.7
CastAddr = 234.6.7.8

//Direct Connect Mode Configuration
[DIRECTCFG]
INTERVAL=3000
DEFAULT=1*192.168.2.135

代碼示例如下:

public class INIFile {
 
    public static void main(String[] args) {
//注意此處讀取的配置的字符串的拼接
        System.out.println(getIniParaValue("DIRECTCFG.INTERVAL", "DSNetComm.ini"));
    }
 
     
    public static String getIniParaValue(String parameterName , String iniPath){
        String parameterValve = null;
         
        try {
            File iniFile = new File(iniPath);
            HierarchicalINIConfiguration inicfg = new HierarchicalINIConfiguration(iniFile);
            parameterValve = inicfg.getString(parameterName);
        } catch (ConfigurationException e) {
            e.printStackTrace();
        }
        return parameterValve;
    }
}

xml類型配置文件的讀取

(采用xpath方式讀取 XMLConfiguration getString(String Key)方法)

(解析xml配置文件的時候可以采用兩種解析方式:1)普通的解析方式;2)xpath方式)

(采用xpath解析xml配置文件,需要依賴:commons-collections-3.2.2.jar、commons-jxpath-1.3.jar)

延伸:

https://blog.csdn.net/chenyan6557/article/details/100717378

example.xml配置文件內(nèi)容:

<?xml version="1.0" encoding="utf-8"?>
<college>
    <student name="foo" gender="M">
        <score course="Algorithm">97</score>
        <score course="Operating System">97</score>
    </student>
    <student name="bar" gender="F">
        <score course="Algorithm">86</score>
        <score course="Operating System">91</score>
    </student>
    <teacher>
        <name>Tom</name>
        <age>31</age>
   </teacher>
</college>

代碼示例如下:(普通的解析方式)

public class XmlFile {
    public static void main(String[] args) {
        String xmlFilePath = "college.xml";
        //獲取老師的姓名及年齡信息
        String str1 = "teacher.name";
        String str2 = "teacher.age";
        System.out.println(getXmlParaValue(str1, xmlFilePath));
        System.out.println(getXmlParaValue(str2, xmlFilePath));
         
        //獲取第一個學(xué)生的姓名&性別
        String str3_name = "student(0)[@name]";
        String str3_gender = "student(0)[@gender]";
        System.out.println(getXmlParaValue(str3_name, xmlFilePath));
        System.out.println(getXmlParaValue(str3_gender, xmlFilePath));
         
        //獲取第二個學(xué)生的姓名&性別
        String str4_name = "student(1)[@name]";
        String str4_gender = "student(1)[@gender]";
        System.out.println(getXmlParaValue(str4_name, xmlFilePath));
        System.out.println(getXmlParaValue(str4_gender, xmlFilePath));
         
        //獲取第一個學(xué)生的第一門課程的名稱及成績
        String str5_courseName = "student(0).score(0)[@course]";
        String str5 = "student(0).score(0)";
        System.out.println("第一個學(xué)生的[ " + getXmlParaValue(str5_courseName, xmlFilePath) + " ]課程的考試成績?yōu)椋? + getXmlParaValue(str5, xmlFilePath));
         
        //獲取所有的學(xué)生的姓名
        String studentName = "student[@name]";
        List<Object> studentNameList = getXmlParaValues(studentName, xmlFilePath);
        for (Object object : studentNameList) {
            System.out.println(object);
        }
         
        //獲取第一個學(xué)生的所有的考試成績
        String str6 = "student(0).score";
        List<Object> xmlParaValues = getXmlParaValues(str6, xmlFilePath);
        System.out.println(xmlParaValues.size());
        for (Object object : xmlParaValues) {
            System.out.println(object);
        }
    }
     
     
     
     // 獲取xml配置文件中單個配置屬性的值
      
    public static String getXmlParaValue(String parameterName , String xmlFilePath){
        String parameterVal = null;
         
        try {
            File xmlFile = new File(xmlFilePath);
            XMLConfiguration xmlCfg = new XMLConfiguration(xmlFile);
             
            parameterVal = xmlCfg.getString(parameterName);
             
        } catch (ConfigurationException e) {
            e.printStackTrace();
        }
         
        return parameterVal;
    }
     
     
     //批量獲取同配置屬性的參數(shù)值(返回的是List<Object>)
      
    public static List<Object> getXmlParaValues(String parameterName , String xmlFilePath){
        List<Object> list = new ArrayList<Object>();
         
        try {
            File xmlFile = new File(xmlFilePath);
            XMLConfiguration xmlCfg = new XMLConfiguration(xmlFile);
             
            list = xmlCfg.getList(parameterName);
             
        } catch (ConfigurationException e) {
            e.printStackTrace();
        }
         
         
        return list;
    }
     
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。