Java 加载配置文件的方式
一 使用原生方式读取配置文件
1 文件系统加载
Java代码
InputStream in = new FileInputStream("config.properties");
Properties p = new Properties();
p.load(in);
2 类加载方式
A 与类同级目录
Java代码
InputStream in = Main.class.getResourceAsStream("config.properties");
B 在类的下一级目录
Java代码
InputStream in =
Main.class.getResourceAsStream("resource/config.properties");
C 指定加载资源配置文件的classes相对路径
Java代码
InputStream in =
Main.class.getResourceAsStream("/test/resource/config.properties");
注意事项:如上以/开头的是指从根目录开始加载。
D 使用类加载器的方式
Java代码
InputStream in = Main.class.getClassLoader().
getResourceAsStream("test/resource/config.properties");
E 资源配置文件在classes下
Java代码
InputStream in =
Main.class.getClassLoader().getResourceAsStream("config.properties");
二 使用apache提供的功能组件
参照网上相关资料
1 读取properties文件的方式
示例代码
Java代码
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("system-config.properties"));
String ip = config.getString("ip");
String[] colors = config.getStringArray("colors.pie");
for (int i = 0; i < colors.length; i++) {
System.out.println(colors[i]);
}
List<?> colorList = config.getList("colors.pie");
for (int i = 0; i < colorList.size(); i++) {
System.out.println(colorList.get(i));
}
}
}
配置文件代码:
ip=10.10.1.1
colors.pie = #FF0000, #00FF00, #0000FF
2 读取XML文件的方式
Java代码
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("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);
for (int i = 0; i < roles.size(); i++) {
System.out.println("Role: " + roles.get(i));
}
}
}
Xml文件代码:
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>
示例2
Java代码
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;
public class XmlConfigurationExample {
public static void main(String[] args) throws Exception,
ConfigurationException {
String resource = "main/global.xml";
Configuration config = new XMLConfiguration(resource);
// 只有new一个XMLConfiguration的实例就可以了.
List<?> startCriteria = config.getList("start-criteria.criteria");
for (int i = 0; i < startCriteria.size(); i++) {
System.out.println("Role: " + startCriteria.get(i));
}
int horsepower = config.getInt("horsepower");
System.out.println(horsepower);
}
}
global.xml文件代码
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>
示例3:混合读取二种文件
Java代码
import java.util.List;
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 {
public static void main(String[] args) throws ConfigurationException {
CompositeConfiguration config = new CompositeConfiguration();
config.addConfiguration(new PropertiesConfiguration(
"main/global.properties"));
config.addConfiguration(new XMLConfiguration("main/global.xml"));
List<?> startCriteria = config.getList("start-criteria.criteria");
int horsepower = config.getInt("horsepower");
for (int i = 0; i < startCriteria.size(); i++) {
System.out.println("Role: " + startCriteria.get(i));
}
System.out.println(horsepower);
System.out.println("Speed: " + config.getFloat("speed"));
System.out.println("Names: " + config.getString("name"));
}
}
Xml如上所示,global.properties文件如下:
speed=50
name=Default User
示例4
Java代码
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 {
ConfigurationFactory factory = new ConfigurationFactory();
URL configURL = PropertiesXmlConfigurationExample.class.getResource("additional-xml-configuration.xml");
factory.setConfigurationURL(configURL);
Configuration config = factory.getConfiguration();
List<?> startCriteria = config.getList("start-criteria.criteria");
for (int i = 0; i < startCriteria.size(); i++) {
System.out.println("Role: " + startCriteria.get(i));
}
int horsepower = config.getInt("horsepower");
System.out.println("Horsepower: " + horsepower);
System.out.println("Speed: " + config.getFloat("speed"));
System.out.println("Names: " + config.getString("name"));
}
}
additional-xml-configuration.xml文件内容:
Xml代码
<?xml version="1.0" encoding="ISO-8859-1" ?>
<configuration>
<properties fileName="global.properties"/>
<xml fileName="global.xml"/>
</configuration>
相关API说明:
# PropertiesConfiguration 从一个 property 文件中加载配置 .
# XMLConfiguration 从 XML 文件中加载配置信息 .
# XMLPropertyListConfiguration 也可以读取 XML 被 Mac OSX 使用变量 .
# JNDIConfiguration 利用 jndi 树的一个键值,可以返回一个值,这个值来作为配置信息
# BaseConfiguration 访问内存中的一个配置对象 .
# HierarchicalConfiguration 一个在内存中的配置对象,这个对象可以处理复杂的数据结构 .
# SystemConfiguration 一个利用系统属性的配置信息
# ConfigurationConverter 把 java.util.Properties 或者 org.apache.collections.commons.ExtendedProperties 转化为一个 Configuration 对象
参考资料:
common-configuration读取xml,properties文件
http://javamy.iteye.com/blog/252619
使用apache common configuration读取配置文件或修改配置文件
http://www.suneca.com/article.asp?id=9
若转载请注明出处!若有疑问,请回复交流!