Java中加载配置文件方式的总结
当需要在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;}
}
Java中加载配置文件方式的总结相关推荐
- java配置文件工具类,java项目加载配置文件的工具类
java项目加载配置文件的工具类 package com.loadproperties; import java.io.IOException; import java.io.InputStream; ...
- java动态加载配置文件
最近项目中需要做定时任务,即定时数据库的备份.定时时间用户可以在界面中配置,要求配置修改好立即生效. 想不到什么好办法.下面是一种实现思路 把用户配置的时间存到properties配置文件中,定时任务 ...
- java 中加载图片
初学java在网上找了好多关于在applet中加载图片的程序,感觉比较多而乱,下面是个简单的示例: import java.awt.Frame; import java.applet.*; imp ...
- spring项目中加载配置文件
spring配置properties或其他文件的方式: 1.通过spring的自带配置 <!-- properties配置文件加载 --><bean class="org. ...
- java中加载驱动程序的方法_下面哪一项不是加载驱动程序的方法?
JDBC连接数据库 创建一个以JDBC连接数据库的程序,包含7个步骤: 1.加载JDBC驱动程序: 在连接数据库之前,首先要加载想要连接的数据库的驱动到JVM(Java虚拟机),这通过java.lan ...
- Java中加载properties文件的6种方法
.使用java.util.Properties类的load()方法 示例: InputStream in = lnew BufferedInputStream(new FileInputStream( ...
- Java中加载图片并显示
目录 一.首先获得图片 二.将图片显示在界面中 三.完整代码展示 一.首先获得图片 关于图片的加载方式有很多种,这里我只介绍一种,毕竟太多记不住. URL url1=deskball.class.ge ...
- java 注解加载配置文件_Spring的Java配置方式和读取properties配置文件
java配置是spring4.x推荐的配置方式,可以完全替代xml配置. 1.注解 @Configuration 和 @Bean spring的java配置方式是通过@Configuration和@B ...
- java 加载资源_在Java中加载资源的首选方式
慕仰8121524 我搜索三个地方,如下所示.欢迎评论.public URL getResource(String resource){ URL url ; //Try with the Thread ...
最新文章
- MySQL数据库性能优化之一
- java set如何判断重复_set 怎么用iterator()方法来区分重复与否
- 快速上手RaphaelJS--RaphaelJS_Starter翻译(二)
- CV:Visual Studio 2015版本+CUDA8.0+Cudnn8.0+OpenCV 3.1.0版本完美解决的详细攻略
- 八、Linux 常用 Shell 命令,控制台的快捷键以及 Shell 编程(中)
- hashmap删除指定key_HashTable和HashMap的区别详解
- BZOJ3172 TJOI2013 单词
- 【STC15库函数上手笔记】2、GPIO
- 3. 什么是icmp?icmp与ip的关系_公共关系与人际交往能力自主模式课程相关
- C# 委托事件传递参数
- 吴恩达神经网络和深度学习-学习笔记-8-梯度消失与爆炸 + 梯度检测
- Thread 线程基础之-线程池ThreadPool一
- openEuler 高校开发者大赛报名启动!广阔天地,码出不凡
- JS 表单submit() 提交无效的问题
- 代码文件夹带有红色感叹号_接口测试平台代码实现7:菜单的开发
- shell php的守护进程,实例详解shell编写守护进程的方法
- java代码实现端口是否ping通
- 苹果说全是假的,市面不存在原装贴膜
- 将Python Pandas DataFrame写入docx文档指定位置
- KnockoutJs 进阶学习