当需要在Java中加载配置文件的时候,有几个选项提供选择:
· File
· Class.getResourceAsStream
· Class.getClassLoader().getResourceAsStream
· Class.getResouce
· ResourceBundle
· Thread.currentThread().getContextClassLoader().getResourceAsStream
在一般情况下,一个配置文件可以有任意复杂的结构(例如, XML模式定义文件) 。为了简单起见,在本文中假定我们需要处理只是包含Key-Value的properties文件
File
File访问文件的时候依赖的是native的特性,使用的是绝对路径(new File(“absolute file path”))。对于一个非常简单的J2SE应用而言, 通过绝对磁盘路径的方式使用File访问properties文件是个比较好的选择。
在J2EE的环境中,由于部署环境的不确定性,使用File的访问方式会带来很多的问题,例如从Window平台转向Linux平台时,绝对路径就需要改变。
除非万不得已,否则应该拒绝使用这种方式来访问properties文件
Class Loader
让我们来展示一个更好的选择:通过Class Loader的方式来加载Properties文件,这种方式通过使用Class文件的加载机制来加载Properties文件,从根本上解决了因为使用磁盘的绝对路径带来的兼容性问题。
举个例子来说明,你需要在A.jar/TestA.class中访问B.jar/test.properties文件。您可以在运行TestA.class的时候把B.jar添加到classpath中,或者是直接把B.jar放到JAVA_HOME/jre/lib中。
关键的一点是,如果你的程序可以访问到B.jar中的class,那么就可以访问到test.properties.
通过Class Loader机制加载properties文件有多种实现的方式(文章的开头列出的方式中,除了File方式外,别的都是属于这一类),每种方式在使用的过程中都存在差异,下表简单的说明了这些方式的差异
Method | Parameter format | Lookup failure behavior | Usage example |
ClassLoader. | · "/"-separated names; · no leading "/" (all names are absolute) | Silent (returns null) | this.getClass().getClassLoader() |
Thread.currentThread().getContextClassLoader().getResourceAsStream | · "/"-separated names; · no leading "/" (all names are absolute) | Silent (returns null) | Thread.currentThread().getContextClassLoader().getResourceAsStream("com/eric/io/" + fileName) |
Class. | · "/"-separated names · leading "/" indicates absolute names · all other names are relative to the class's package | Silent (returns null) | this.getClass() |
Class.getResource | · "/"-separated names · leading "/" indicates absolute names · all other names are relative to the class's package | Silent (returns null) | URL url = LoadConfigFile.class.getResource(fileName); new FileInputStream(new File(url.getFile())); |
ResourceBundle. | · "."-separated names · all names are absolute · .properties suffix is implied | Throws unchecked | ResourceBundle.getBundle |
简单的例子
前面表格列出了各种加载机制的差异,下面通过一个具体的例子来做说明
首先文件结构如下,在src/com/eric/io这个包下包含LoadConfigFile.java以及list2.properties两个文件,在src下包含list3.properties一个文件
|--project
|--src
|--com.eric.io
|--LoadConfigFile.java
|--list2.properties
|--list3.properties
下面的程序用来说明如何使用以上的5种方式加载list2/list3.properties
package com.eric.io;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Enumeration;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.ResourceBundle;
/**
*
* Read properties by different mechanism
*
* @author aihua.sun
*/
public class LoadConfigFile {
private static String samePackageProperties = "list2.properties";
private static String differentPackageProperties = "list3.properties";
public static void main(String args[]) throws IOException {
loadSamePackageProperties(samePackageProperties);
loadDifferentPackageProperties(differentPackageProperties);
}
private static void loadDifferentPackageProperties(String differentPackagePropertiesName) throws IOException,
FileNotFoundException {
loadProperties(loadFileByClass("/" + differentPackagePropertiesName));
loadProperties(loadFileByClassResouce("/" + differentPackagePropertiesName));
loadProperties(loadFileByClassLoader(differentPackagePropertiesName));
loadProperties(loadFileByThreadContent(differentPackagePropertiesName));
loadProperties(loadFilesByResourceBundle("list3"));
}
private static void loadSamePackageProperties(String samePackagePropertiesName) throws IOException,
FileNotFoundException {
loadProperties(loadFileByClassLoader("com/eric/io/" + samePackagePropertiesName));
loadProperties(loadFileByThreadContent("com/eric/io/" + samePackagePropertiesName));
loadProperties(loadFileByClass(samePackagePropertiesName));
loadProperties(loadFileByClassResouce(samePackagePropertiesName));
loadProperties(loadFilesByResourceBundle("com.eric.io.list2"));
}
public static void loadProperties(Properties properties) {
for (Entry entry : properties.entrySet()) {
System.out.println(entry.getKey() + ":" + entry.getValue());
}
System.out.println();
}
/**
* "."-separated names; all names are absolute; .properties suffix is implied
*
* @return
*/
public static Properties loadFilesByResourceBundle(String propertiesFile) {
System.out.println("Generate By resouce Bundle");
ResourceBundle resourceBundle = ResourceBundle.getBundle(propertiesFile);
Properties result = new Properties();
for (Enumeration<?> keys = resourceBundle.getKeys(); keys.hasMoreElements();) {
final String key = (String) keys.nextElement();
final String value = resourceBundle.getString(key);
result.put(key, value);
}
return result;
}
private static Properties loadFileByClassResouce(String propertiesFile) throws FileNotFoundException, IOException {
System.out.println("Generate By resouce");
URL url = LoadConfigFile.class.getResource(propertiesFile);
return generatePropertiesByIS(new FileInputStream(new File(url.getFile())));
}
/**
* In Class.getResourceAsStream(path), the path is interpreted as a path local to the package of the class you are
* calling it from. For example calling, String.getResourceAsStream("myfile.txt") will look for a file in your
* classpath at the following location: "java/lang/myfile.txt". If your path starts with a /, then it will be
* considered an absolute path, and will start searching from the root of the classpath. So calling
* String.getResourceAsStream("/myfile.txt") will look at the following location in your in your class path
* ./myfile.txt.
*
* @return
* @throws IOException
*/
public static Properties loadFileByClass(String propertiesFile) throws IOException {
System.out.println("Generate By Class");
return generatePropertiesByIS(LoadConfigFile.class.getResourceAsStream(propertiesFile));
}
/**
* ClassLoader.getResourceAsStream(path) will consider all paths to be absolute paths. So calling
* String.getClassLoader().getResourceAsString("myfile.txt") and
* String.getClassLoader().getResourceAsString("/myfile.txt") will both look for a file in your classpath at the
* following location: ./myfile.txt.
*
* @return
* @throws IOException
*/
public static Properties loadFileByClassLoader(String propertiesFile) throws IOException {
System.out.println("Generate By ClassLoader");
return generatePropertiesByIS(LoadConfigFile.class.getClassLoader().getResourceAsStream(propertiesFile));
}
/**
* In your case, you are loading the class from an Application Server, so your should use
* Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName) instead of
* this.getClass().getClassLoader().getResourceAsStream(fileName).
*
* @return
* @throws IOException
*/
public static Properties loadFileByThreadContent(String propertiesFile) throws IOException {
System.out.println("Generate By ThreadContent");
return generatePropertiesByIS(Thread.currentThread().getContextClassLoader()
.getResourceAsStream(propertiesFile));
}
/**
* Generate Properties Object by InputStream Object
*
* @param is
* source input stream
* @return
* @throws IOException
*/
private static Properties generatePropertiesByIS(InputStream is) throws IOException {
Properties result = new Properties();
result.load(is);
return result;
}
}