首页 > 代码库 > commons-configuration读取配置文件
commons-configuration读取配置文件
关键工具类:
- import org.apache.commons.configuration.CompositeConfiguration;
- import org.apache.commons.configuration.ConfigurationException;
- import org.apache.commons.configuration.PropertiesConfiguration; //读取properties配置文件
- import org.apache.commons.configuration.XMLConfiguration; //读取xml文件
我们在实际的开发设计中,通常要读各种各样的配置文件,十分烦琐.然而commons-configuration-1.0.jar为我们提供了非常方便的操作方式实例如下:1.读取XML文件中内容
[java] view plain copy print?- import java.util.List;
- import org.apache.commons.configuration.Configuration;
- import org.apache.commons.configuration.XMLConfiguration;
- public class XmlConfigurationExample {
- public static void main(String[] args) throws Exception {
- String resource = "com/discursive/jccook/configuration/global.xml";
- Configuration config = new XMLConfiguration(resource);
- //只有new一个XMLConfiguration的实例就可以了.
- List startCriteria = config.getList("start-criteria.criteria");
- int horsepower = config.getInt("horsepower");
- System.out.println( "Start Criteria: " + startCriteria );
- System.out.println(horsepower);
- }
- }
[html] view plain copy print?- global.xml
- <?xml version="1.0" encoding="ISO-8859-1" ?>
- <engine-config>
- <start-criteria>
- <criteria>
- Temperature Above -10 Celsius
- </criteria>
- <criteria>
- Fuel tank is not empty
- </criteria>
- </start-criteria>
- <volume>4 Liters</volume>
- <horsepower>42</horsepower>
- </engine-config>
2.读取properties文件[java] view plain copy print?- import org.apache.commons.configuration.Configuration;
- import org.apache.commons.configuration.PropertiesConfiguration;
- public class PropertiesConfigurationExample {
- public static void main(String[] args) throws Exception {
- Configuration config = new PropertiesConfiguration(
- "com/discursive/jccook/configuration/global.properties" );
- System.out.println( "Speed: " + config.getFloat("speed"));
- System.out.println( "Names: " + config.getString("name"));
- }
- }
[plain] view plain copy print?- global.properties
- threads.maximum=50
- threads.minimum=20
- timeout=15.52
- interactive=true
- color=red
- speed=50
- name=Default User
- email=default@email.com
- region=Earth
3.当有多个配置文件时,就利用ConfigurationFactory对象来访问多个不同的配置资源[java] view plain copy print?- import java.net.URL;
- import java.util.List;
- import org.apache.commons.configuration.Configuration;
- import org.apache.commons.configuration.ConfigurationFactory;
- public class PropertiesXmlConfigurationExample {
- public static void main(String[] args) throws Exception {
- PropertiesXmlConfigurationExample example = new PropertiesXmlConfigurationExample();
- ConfigurationFactory factory = new ConfigurationFactory();
- URL configURL = example.getClass().getResource("additional-xml-configuration.xml");
- factory.setConfigurationURL( configURL );
- Configuration config = factory.getConfiguration();
- List startCriteria = config.getList("start-criteria.criteria");
- System.out.println( "Start Criteria: " + startCriteria );
- int horsepower = config.getInt("horsepower");
- System.out.println( "Horsepower: " + horsepower );
- }
- }
[html] view plain copy print?- additional-xml-configuration.xml
- <?xml version="1.0" encoding="ISO-8859-1" ?>
- <configuration>
- <properties fileName="global.properties"/>
- <xml fileName="global.xml"/>
- </configuration>
4.CompositeConfiguration,手动加上两个配置文件[java] view plain copy print?- import org.apache.commons.configuration.CompositeConfiguration;
- import org.apache.commons.configuration.ConfigurationException;
- import org.apache.commons.configuration.PropertiesConfiguration;
- import org.apache.commons.configuration.XMLConfiguration;
- public class Test {
- /**
- * @param args
- * @throws ConfigurationException
- */
- public static void main(String[] args) throws ConfigurationException {
- // TODO Auto-generated method stub
- CompositeConfiguration config = new CompositeConfiguration();
- config.addConfiguration(new PropertiesConfiguration(
- "com/discursive/jccook/configuration/global.properties" ));
- config.addConfiguration( new XMLConfiguration
- ("com/discursive/jccook/configuration/global.xml"));
- List startCriteria = config.getList("start-criteria.criteria");
- int horsepower = config.getInt("horsepower");
- System.out.println( "Start Criteria: " + startCriteria );
- System.out.println(horsepower);
- System.out.println( "Speed: " + config.getFloat("speed"));
- System.out.println( "Names: " + config.getString("name"));
- }
- }
- 来源: http://blog.csdn.net/zztp01/article/details/6707928
来自为知笔记(Wiz)
附件列表
commons-configuration读取配置文件
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。