我们在实际的开发设计中,通常要读各种各样的配置文件,十分烦琐.然而commons-configuration-1.0.jar为我们提供了非常方便的操作方式实例如下:1.读取XML文件中内容
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);
}
}
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文件
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"));
}
}
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对象来访问多个不同的配置资源
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 );
}
}
additional-xml-configuration.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<configuration>
<properties fileName="global.properties"/>
<xml fileName="global.xml"/>
</configuration>
4.CompositeConfiguration,手动加上两个配置文件
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"));
}
}
分享到:
相关推荐
集群所述cnnclustering Python包提供了一个灵活的接口聚类算法使用C ommon-Ñearest-Ñeighbours。 虽然该方法可以应用于任意数据,但此实现是在“分子动力学”模拟的处理轨迹背景之前完成的。 在这种情况下,聚类...
Makefile的核心作用是管理项目的编译规则,它告诉make程序哪些文件是依赖文件,哪些文件是目标文件,以及如何从依赖文件生成目标文件。以下将详细介绍Makefile的基本知识点,包括其概念、作用、编写规则和C/C++编译...
The Common Public Radio Interface (CPRI) is an industry cooperation aimed at defining publicly available specifications for the key internal interface of radio base stations, such as eCPRI connecting ...
6. **配置文件处理**:FastDFS的配置文件解析工作也是由libfastcommon完成的,它能够解析配置文件,将配置项转化为程序可以使用的结构体。 7. **时间处理**:libfastcommon还提供了时间相关的函数,如获取当前时间...
Java的Arcade-Csdb(C)ommon(S)wing(D)atabound(B)eans
自动图像字幕 介绍 图像字幕处理是自动对看不见的图像进行...微软Çommon在CO NTEXTÖbjects(MS COCO)数据集是用于场景理解大规模数据集。 该数据集通常用于训练和基准化对象检测,分段和字幕算法。 您可以在或阅
我们使用微软Çommon在CO NTEXT(MS COCO)O bjects为这个项目。 它是用于场景理解的大规模数据集。 该数据集通常用于训练和基准化对象检测,分段和字幕算法。 有关下载数据的说明,请参见下面的“数据”部分。代码...