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 ...
最新文章
- MLIR中间表示和编译器框架
- linux 命令行简介
- jQuery事件之鼠标事件
- python爬取网页公开数据_如何用Python爬取网页数据
- python 查找文件名包含指定字符串
- (王道408考研数据结构)第八章排序-第三节1:简单选择排序
- Android 编码规范
- 计算机仿真电路实验感想,电路计算机仿真 实验报告.doc
- win10家庭版无法安装mysql_Win10安装MySQL
- hibernate4中使用Session doWork()方法进行jdbc操作(代码)
- 基于SSM的宠物医院信息管理系统javaweb毕业设计项目源码论文
- 微信管理软件哪个比较不错
- Go原生插件使用问题全解析
- 传说中的ACM大牛们
- 编辑中的word变成只读_word只读模式怎么取消,word只读模式怎么改
- ajax deferred 用法
- error LNK2019: 无法解析的外部符号 XXXXXXXXX,该符号在函数XXX中被引用
- Python爬虫-使用Jupyter爬虫
- Linux内核的五大模块
- 高数:第五章(同济大学第七版)
热门文章
- 怎样配置FTP服务器
- 【手写信息搜集工具】ThunderSearch 闪电搜索器
- 质量管理五大工具详解
- vc_redist 又名VC runtime library,或MSCVRT
- matlab中变压器的励磁阻抗参数设置,【请教变压器零序阻抗与正序或负序阻抗之间的比例关系,如1台变压器的阻抗4.5%,哪他的零序阻抗通常是多少?】_阻抗 变压器_全球新能源网...
- HDLC协议基本功能模拟实现 (C语言)
- 【原创】从头开始,使用安卓系统WebView做一个功能强大的Epub阅读器(四)
- Android安装的误区
- 微信小程序 使用.wxs在.wxml中分割字符串渲染多条数据
- golang Opts Pattern