java配置文件


(2010-03-04 20:53:29)
标签:

name

可以通过Commons Configuration访问存储于属性文件和XML文档中的配置信息, 依赖的包还有Commons Language, Commons Collections和Commons Logging.

1. 通过属性文件配置应用程序

属性文件作为配置文件, 读取简单, 但配置内容不灵活.

例如有配置文件config.properties如下:

no=237
names=kiyho,sinkiang
correct=false

可以用以下代码读取:

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;

public class PropertiesConfigDemo {
public static void main(String[] args) {
try {
Configuration config = new PropertiesConfiguration(
"config.properties");
System.out.println("score: " + config.getFloat("score"));
System.out.println("names: " + config.getList("names"));
System.out.println("correct: " + config.getBoolean("correct"));
} catch (ConfigurationException e) {
e.printStackTrace();
}
}
}

感觉挺不爽, 用个configuration, 还要依赖其他3个commons包.

另外可以通过util包的Properties类实现配置读取, 只是读取到的都是String类型, 需要自己转换.

import java.io.IOException;
import java.util.Properties;

public class PropertiesDemo {
public static void main(String[] args) {
try {
Properties properties = new Properties();
properties.load(Thread.currentThread().getContextClassLoader()
.getResourceAsStream("config.properties"));
System.out.println("score: " + properties.getProperty("score"));
System.out.println("names: " + properties.getProperty("names"));
System.out.println("correct: " + properties.getProperty("correct"));
} catch (IOException e) {
e.printStackTrace();
}
}
}

2. 通过XML配置应用程序

假设有如下xml配置文件config.xml:

<?xml version="1.0" encoding="utf-8" ?>
<config>
<name>kiyho</name>
<name>sinkiang</name>
<info>
<age>100</age>
</info>
</conifig>

可以用以下代码访问:

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;

public class XmlConfigDemo {
public static void main(String[] args) {
try {
XMLConfiguration config = new XMLConfiguration("config.xml");
System.out.println(config.getList("name"));
System.out.println(config.getInt("info.age"));
} catch (ConfigurationException e) {
e.printStackTrace();
}
}
}

XML配置文件读取除了用Configuation外, 还可以用JDOM读取:

import java.io.IOException;
import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;

public class JdomXmlConfigDemo {
public static void main(String[] args) {
ClassLoader classLoader = Thread.currentThread()
.getContextClassLoader();
try {
Document document = new SAXBuilder().build(classLoader
.getResourceAsStream("config.xml"));
List<Element> names = document.getRootElement().getChildren("name");
String age = document.getRootElement().getChild("info").getChild(
"age").getTextTrim();
for (Element name : names)
System.out.println("name: " + name.getTextTrim());
System.out.println("age: " + age);
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

不依赖任何Commons包, 只要到jdom包, 不过读取到的都是String, 需要自己转换.

3. 复合配置

多个配置文件, 假设有3个配置文件: global.properties, local.properties, user.properties, 其中user优先于local, local优先于global. 那么用以下这3个配置文件和1个引用配置文件可实现复合配置.

cofigs.xml内容如下:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<properties fileName="user.properties" />
<properties fileName="local.properties" />
<properties fileName="global.properties" />
</configuration>

global.properties内容如下:

name=Default User
age=0
speed=10

local.properties内容如下:

name=kiyho
age=20

user.properties内容如下:

age=100

程序读取如下:

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.ConfigurationFactory;

public class ConfigsDemo {
public static void main(String[] args) {
ConfigurationFactory factory = new ConfigurationFactory();
factory.setConfigurationFileName("configs.xml");

try {
Configuration config = factory.getConfiguration();

System.out.println("name: " + config.getString("name"));
System.out.println("age: " + config.getInt("age"));
System.out.println("speed: " + config.getFloat("speed"));
} catch (ConfigurationException e) {
e.printStackTrace();
}
}
}

你可能感兴趣的:(java,apache,thread,xml)