Jakarta-Common-Configuration使用笔记
时间:2011-01-28 csdn博客 沈斌
这是一个读取配置文件的工具包。
下载地址 : http://commons.apache.org/configuration/
A.读取XML配置文件:
<config>
<ip>10.10.1.1</ip>
<account>jini</account>
<password>jakarta99</password>
<roles>
<role>admin</role>
<role>manager</role>
<role>user</role>
</roles>
</config>
测试代码:
package demo;
import java.util.List;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;
public class XMLConfigurationDemo ...{
public static void main(String[] args) throws ConfigurationException ...{
Configuration config = new XMLConfiguration(XMLConfigurationDemo.class
.getResource("/demo/system-config.xml"));
String ip = config.getString("ip");
String account = config.getString("account");
String password = config.getString("password");
List<?> roles = config.getList("roles.role");
System.out.println("IP: " + ip);
System.out.println("Account: " + account);
System.out.println("Password: " + password);
System.out.println("★★★★★ Roles Begin ★★★★★");
for (int i = 0; i < roles.size(); i++) ...{
System.out.println("Role: " + roles.get(i));
}
System.out.println("★★★★★ Roles End ★★★★★");
}
}
B.读取Properties配置文件:
ip=10.10.1.1
account=jini
password=jakarta99
role1=admin
role2=manager
role3=user
colors.pie = #FF0000, #00FF00, #0000FF
Jakarta-Common-Configuration使用笔记(2)
时间:2011-01-28 csdn博客 沈斌
测试代码:
package demo;
import java.util.List;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
public class PropertiesConfigurationDemo ...{
public static void main(String[] args) throws ConfigurationException ...{
Configuration config = new PropertiesConfiguration(
PropertiesConfigurationDemo.class
.getResource("/demo/system-config.properties"));
String ip = config.getString("ip");
String account = config.getString("account");
String password = config.getString("password");
String role1 = config.getString("role1");
String role2 = config.getString("role2");
String role3 = config.getString("role3");
System.out.println("IP: " + ip);
System.out.println("Account: " + acc
|