当需要在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.
getResourceAsStream()
|
· "/"-separated names;
· no leading "/" (all names are absolute)
|
Silent (returns null)
|
this.getClass().getClassLoader()
.getResourceAsStream
("some/pkg/resource.properties")
|
Thread.currentThread().getContextClassLoader().getResourceAsStream (Instead of instead of ClassLoader.
getResourceAsStream() in J2EE Envrionment)
|
· "/"-separated names;
· no leading "/" (all names are absolute)
|
Silent (returns null)
|
Thread.currentThread().getContextClassLoader().getResourceAsStream("com/eric/io/" + fileName)
|
Class.
getResourceAsStream()
|
· "/"-separated names
· leading "/" indicates absolute names
· all other names are relative to the class's package
|
Silent (returns null)
|
this.getClass()
.getResourceAsStream
("resource.properties")
|
Class.getResource (return URL Object)
|
· "/"-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.
getBundle()
|
· "."-separated names
· all names are absolute
· .properties suffix is implied
|
Throws unchecked
java.util.MissingResourceException
|
ResourceBundle.getBundle
("some.pkg.resource")
|
简单的例子
前面表格列出了各种加载机制的差异,下面通过一个具体的例子来做说明
首先文件结构如下,在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;
}
}
分享到:
相关推荐
通过单例模式实例化获取propertyUtil 工具包实例,高效加载配置文件,java语言编写。通过单例模式实例化获取propertyUtil 工具包实例,高效加载配置文件,java语言编写。通过单例模式实例化获取propertyUtil 工具包...
NULL 博文链接:https://liuzidong.iteye.com/blog/831440
java ibaties sqlMap配置文件 java ibaties sqlMap配置文件 java ibaties sqlMap配置文件 java ibaties sqlMap配置文件 java ibaties sqlMap配置文件
NULL 博文链接:https://ljm653467.iteye.com/blog/2163384
java加载属性配置文件[properties文件]什么是properties文件为什么要使用properties文件使用java加载properties文件的两种方式使用类的加载器获得输入流加载文件getResourceAsStream()介绍使用文件输入流加载文件...
应用程序在启动的时候,可能会读取应用所需要的配置文件,如果说配置文件的内容不是一成不变的,在需要...假如,这个配置文件是我们的应用完全可控的,我们可以在不重启服务器的情况下,把新的配置文件数据加载起来。
Spring动态加载配置文件
properties文件获取工具类:静态加载properties配置文件,有根据key获取值的方法
java程序中加载动态链接库文件方法,涉及到C语言实现控制底层硬件的方法
SSM框架下的几种加载properties配置文件方式,博客内含介绍https://blog.csdn.net/weixin_42803662
自己开发一个工具类,为第三方应用提供调用接口,但是打包后测试过程中,发现了一个问题就是在用@Value获取配置文件内容的时候,无法获取我们的配置信息,也无法加载我们配置的默认值!具体配置如下: @Value("${...
实现了实体类和配置文件的关系映射、自动加载、自动保存,基于ORM、持久化的思想,使用Java的注解、反射、范型等特性实现。 依赖jtk_util_0.1.jar,地址:http://download.csdn.net/detail/u010475284/7221887
springboot 、java类热加载、jarinjar、动态编译、自动引用依赖
本案例是一个通过静态代码块获取资源属性文件的代码块,从而来提高应用性能。。
主要介绍了Java加载properties文件实现方式详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
Java开发所需要的配置安装文件,jdk,像eclpse,myeclipse,tomcat的安装 都需要配置环境。
自定义Java类加载器demo,自定义了一个classLoader,重写了loadClass 和findClass,注意 loadClass打破了双亲委派机制,所有的类都要在自定义的class文件中找到,而findClass遵循了双亲委派机制
这是一个对于初学者有很大的作用,详细的介绍了MySQL的驱动加载。
Java开发Web程序中修改类文件和配置文件不重启服务器的方法 大家知道,在我们开发web项目的时候重启应用服务器是我们最烦恼的事情,每次修改类文件或者配置文件后,我们必须重启服务器来重新加载,使得我们的修改...
这样,我们就可以在log4j加载配置文件之前,先用System.setProperty ("WORKDIR", WORKDIR);设置好根路径,此操作可通过一初始的servlet进行。 方法二、 可以使用服务器环境变量 log4j的配置文件支持服务器的vm的...