首页 > 代码库 > commons-configuration读取配置文件

commons-configuration读取配置文件

技术分享技术分享

关键工具类:

    1. import org.apache.commons.configuration.CompositeConfiguration; 
    2. import org.apache.commons.configuration.ConfigurationException;   
    3. import org.apache.commons.configuration.PropertiesConfiguration;   //读取properties配置文件
    4. import org.apache.commons.configuration.XMLConfiguration;   //读取xml文件
    5.   

    我们在实际的开发设计中,通常要读各种各样的配置文件,十分烦琐.然而commons-configuration-1.0.jar为我们提供了非常方便的操作方式实例如下:1.读取XML文件中内容

    [java] view plain copy
     print?
    1. import java.util.List;   
    2. import org.apache.commons.configuration.Configuration;   
    3. import org.apache.commons.configuration.XMLConfiguration;   
    4.   
    5. public class XmlConfigurationExample {   
    6.   
    7. public static void main(String[] args) throws Exception {   
    8. String resource = "com/discursive/jccook/configuration/global.xml";   
    9. Configuration config = new XMLConfiguration(resource);   
    10. //只有new一个XMLConfiguration的实例就可以了.   
    11. List startCriteria = config.getList("start-criteria.criteria");   
    12. int horsepower = config.getInt("horsepower");   
    13. System.out.println( "Start Criteria: " + startCriteria );   
    14. System.out.println(horsepower);   
    15. }   
    16. }   

    [html] view plain copy
     print?
    1. global.xml  
    2. <?xml version="1.0" encoding="ISO-8859-1" ?>   
    3.   
    4. <engine-config>   
    5. <start-criteria>   
    6. <criteria>   
    7. Temperature Above -10 Celsius   
    8. </criteria>   
    9. <criteria>   
    10. Fuel tank is not empty   
    11. </criteria>   
    12. </start-criteria>   
    13. <volume>4 Liters</volume>   
    14. <horsepower>42</horsepower>   
    15. </engine-config>   

    2.读取properties文件
    [java] view plain copy
     print?
    1. import org.apache.commons.configuration.Configuration;   
    2. import org.apache.commons.configuration.PropertiesConfiguration;   
    3.   
    4. public class PropertiesConfigurationExample {   
    5.   
    6. public static void main(String[] args) throws Exception {   
    7. Configuration config = new PropertiesConfiguration(   
    8.   
    9. "com/discursive/jccook/configuration/global.properties" );   
    10.   
    11. System.out.println( "Speed: " + config.getFloat("speed"));   
    12. System.out.println( "Names: " + config.getString("name"));   
    13. }   
    14. }   

    [plain] view plain copy
     print?
    1. global.properties  
    2. threads.maximum=50   
    3. threads.minimum=20   
    4. timeout=15.52   
    5. interactive=true   
    6. color=red   
    7. speed=50   
    8. name=Default User   
    9. email=default@email.com   
    10. region=Earth   

    3.当有多个配置文件时,就利用ConfigurationFactory对象来访问多个不同的配置资源
    [java] view plain copy
     print?
    1. import java.net.URL;   
    2. import java.util.List;   
    3.   
    4. import org.apache.commons.configuration.Configuration;   
    5. import org.apache.commons.configuration.ConfigurationFactory;   
    6.   
    7. public class PropertiesXmlConfigurationExample {   
    8.   
    9. public static void main(String[] args) throws Exception {   
    10. PropertiesXmlConfigurationExample example = new PropertiesXmlConfigurationExample();   
    11.   
    12. ConfigurationFactory factory = new ConfigurationFactory();   
    13. URL configURL = example.getClass().getResource("additional-xml-configuration.xml");   
    14. factory.setConfigurationURL( configURL );   
    15.   
    16. Configuration config = factory.getConfiguration();   
    17.   
    18. List startCriteria = config.getList("start-criteria.criteria");   
    19. System.out.println( "Start Criteria: " + startCriteria );   
    20.   
    21. int horsepower = config.getInt("horsepower");   
    22. System.out.println( "Horsepower: " + horsepower );   
    23. }   
    24. }   

    [html] view plain copy
     print?
    1. additional-xml-configuration.xml  
    2. <?xml version="1.0" encoding="ISO-8859-1" ?>   
    3. <configuration>   
    4. <properties fileName="global.properties"/>   
    5. <xml fileName="global.xml"/>   
    6. </configuration>  

    4.CompositeConfiguration,手动加上两个配置文件
    [java] view plain copy
     print?
    1. import org.apache.commons.configuration.CompositeConfiguration;   
    2. import org.apache.commons.configuration.ConfigurationException;   
    3. import org.apache.commons.configuration.PropertiesConfiguration;   
    4. import org.apache.commons.configuration.XMLConfiguration;   
    5.   
    6. public class Test {   
    7.   
    8. /**  
    9. * @param args  
    10. * @throws ConfigurationException  
    11. */   
    12. public static void main(String[] args) throws ConfigurationException {   
    13. // TODO Auto-generated method stub   
    14. CompositeConfiguration config = new CompositeConfiguration();   
    15. config.addConfiguration(new PropertiesConfiguration(   
    16.   
    17. "com/discursive/jccook/configuration/global.properties" ));   
    18. config.addConfiguration( new XMLConfiguration   
    19.   
    20. ("com/discursive/jccook/configuration/global.xml"));   
    21. List startCriteria = config.getList("start-criteria.criteria");   
    22. int horsepower = config.getInt("horsepower");   
    23. System.out.println( "Start Criteria: " + startCriteria );   
    24. System.out.println(horsepower);   
    25.   
    26. System.out.println( "Speed: " + config.getFloat("speed"));   
    27. System.out.println( "Names: " + config.getString("name"));   
    28.   
    29. }   
    30.   
    31. }   

    32. 来源: http://blog.csdn.net/zztp01/article/details/6707928


来自为知笔记(Wiz)


附件列表

     

    commons-configuration读取配置文件