Java读取配置文件的方式-笔记

1       取当前启动文件夹下的配置文件

 

一般来讲启动java程序的时候。在启动的文件夹下会有配置文件

classLoader.getResource("").getFile()  会取到java当前启动项目的文件夹。然后指定相应的配置文件路径就可以比方conf/conf.properties

 

//取当前启动文件夹的配置文件

String   filePath =classLoader.getResource("").getFile()+”conf/conf.properties”;

2       取classpath下的配置文件

在不考虑多个jar包中有同样路径的同名配置文件的话,能够直接取例如以下

 

ClassLoader.getSystemResource("conf/conf.properties")//静态方法
或者
classLoader.getResource("conf/conf.properties") //

 

 

 

小实例

/**
* 取当前启动文件夹的配置文件,假设没有取载入在当前classpath下的。 * @param classLoader
* @param confClassDirectory
* @return
* @throws FileNotFoundException
*/
public static InputStream getInputStreamByConfPath(ClassLoader classLoader,StringconfClassDirectory) throws FileNotFoundException {
if(confClassDirectory.startsWith("/")) {
confClassDirectory= confClassDirectory.substring(1);
}
//取当前启动文件夹的配置文件
String filePath = classLoader.getResource("").getFile()+ confClassDirectory;
InputStream in=null;
File file = new File(filePath);
//假设不存在取当前启动的classpath下的
if(!file.exists()) {
in= classLoader.getResourceAsStream(confClassDirectory);
if(null == in) {
in=classLoader.getResourceAsStream("/"+ confClassDirectory);
}
}else{
in=newFileInputStream(file);
}
return in;
}

 

 

 

 

3       取指定类所在jar包中的配置文件

有的时候A.jar有个文件和B.jar里面也有个文件一样名字一样路径(比方:conf/abc.txt),

假设通过classpath下取conf/abc.txt的仅仅能取到第一个jar包载入的配置文件就是A.Jar,而B.jar的却取不到。

 

假设这个时候想取B.jar中的配置文件能够先找到jar包的位置然后再找到相应的配置文件路径即文件。

能够例如以下实现这样的功能

 

/**
*依据class类找到相应的jar取指定的配置文件
* @param cls
* @param confClassDirectory
* @return
* @throws IOException
*/
public static InputStream getInputStreamByJarFileConfPath(Class<? > cls,StringconfClassDirectory) throws IOException {
String jarPath=cls.getProtectionDomain().getCodeSource().getLocation().getFile();
if(confClassDirectory.startsWith("/")) {
confClassDirectory= confClassDirectory.substring(1);
}
if(jarPath==null) {
returnnull;
}
InputStream in=null;
//推断假设是以jar结束的时候就是在server中使用
if(jarPath.endsWith(".jar")) {
JarFile jarFile = new JarFile(jarPath);
JarEntry entry =jarFile.getJarEntry(confClassDirectory);
in= jarFile.getInputStream(entry);
}else{
//就是可能本地直接依赖项目的使用
File file=new File(jarPath+confClassDirectory);
if(file.exists()) {
in=newFileInputStream(file);
}
}
return in;
}
}

 

当然好像能够通过

Enumeration<URL> urlss=ClassLoader.getSystemResources("conf/conf.properties");
while(urlss.hasMoreElements()){
System.out.println(urlss.nextElement().getFile());
}

取到全部的conf/conf.properties的配置文件。

也能够通过推断路径来推断。

4       取class下的配置文件

 

用Class.getResource不加/(下划线)就是从当前包開始找,一般不推荐。毕竟配置文件配置不方便不说且,maven好像默认打包在src/main/java下的配置文件时不打进去的。

 

 

 

加上/(下划线)就是从classpath根路径找了

 

 

 

Java读取配置文件的方式的更多相关文章

  1. java读取配置文件的几种方法

    java读取配置文件的几种方法 原文地址:http://hbcui1984.iteye.com/blog/56496         在现实工作中,我们常常需要保存一些系统配置信息,大家一般都会选择配 ...

  2. python读取配置文件的方式

    python读取配置文件的方式 1.从config.ini中读取,后缀无所谓,文件名字也无所谓,不过config.ini是常用写法,所谓见名知意 config.ini内容: [global] ip = ...

  3. java读取配置文件常用的四种方式

    配置文件 放置在src下面 obj.properties className=com.store.order.dao.impl.OrderDaoImpl 方式一 @Test public void t ...

  4. java读取配置文件方法以及工具类

    第一种方式 : java工具类读取配置文件工具类 只是案例代码  抓取异常以后的代码自己处理 import java.io.FileNotFoundException; import java.io. ...

  5. java读取配置文件内容

    利用com.typesafe.config包实现 <dependency> <groupId>com.typesafe</groupId> <artifact ...

  6. java读取配置文件(转)

    转载:http://blog.csdn.net/gaogaoshan/article/details/8605887 java 4种方式读取配置文件 + 修改配置文件     方式一:采用Servle ...

  7. JavaWeb中servlet读取配置文件的方式

    我们在JavaWeb中常常要涉及到一些文件的操作,比如读取配置文件,下载图片等等操作.那我们能不能采用我们以前在Java工程中读取文件的方式呢?废话不多说我们来看看下我们以前在Java工程中读取文件是 ...

  8. java读取配置文件

    java 读取文件可以用字节流和字符流. 由于一个汉字占两个字节,所以如果配置文件中有汉字,用字节流读取,会出现乱码. 用字符流则不会出现乱码. 配置文件 b.properties 文件如下: fam ...

  9. Java 读取配置文件数据

    Properties类 Properties类,是一个工具类,包含在java.util包中. 功能:可以保存持久的属性,通常用来读取配置文件或者属性文件,将文件中的数据读入properties对象中, ...

随机推荐

  1. 【codeforces 738E】Subordinates

    [题目链接]:http://codeforces.com/problemset/problem/738/E [题意] 给你一个类似树形的关系; 然后告诉你某个人头顶上有多少个上司numi; 只有fat ...

  2. [TypeScript] Generic Functions, class, Type Inference and Generics

    Generic Fucntion: For example we have a set of data and an function: interface HasName { name: strin ...

  3. 自己主动化測试程序之中的一个自己定义键盘的模拟測试程序(C语言)

    一.測试程序编写说明 我们做的终端设备上运行的是QT应用程序.使用自己定义的键盘接口.经过測试人员长时间的人机交互測试,来确认系统的功能是否满足需求. 如今须要编写一个自己主动化的測试程序,能够依照预 ...

  4. POJ 题目2774 Long Long Message(后缀数组,求最长公共子串长度)

    Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 23696   Accepted: 97 ...

  5. Spoj 1557 Can you answer these queries II 线段树 随意区间最大子段和 不反复数字

    题目链接:点击打开链接 每一个点都是最大值,把一整个序列和都压缩在一个点里. 1.普通的区间求和就是维护2个值,区间和Sum和延迟标志Lazy 2.Old 是该区间里出现过最大的Sum, Oldlaz ...

  6. JSP简单练习-上传文件

    注意:在编写上传文件的代码时,需确保"WEB-INF/lib"下含有jspsmartupload.jar包.否则会出错. jspSmartupload.jar下载 <!-- ...

  7. 3.多线程传参,以及tuple数组

    #include <Windows.h> #include <thread> #include <iostream> #include <tuple> ...

  8. Kettle的四大不同环境工具

    不多说,直接上干货! kettle里有不同工具,分别用于ETL的不同阶段. 初学者,建议送Spoon开始.高手,是四大工具都会用. Sqoop: 图形界面工具,快速设计和维护复杂的ETL工作流.集成开 ...

  9. Eclipse里Tomcat报错:Document base ……does not exist or is not a readable directory(图文详解)

    问题描述: 严重: Error starting static Resourcesjava.lang.IllegalArgumentException: Document base D:\Code\M ...

  10. Ubuntu 16.04下安装64位谷歌Chromium(Chrome)浏览器

    在命令行下输入: sudo add-apt-repository ppa:a-v-shkop/chromium sudo apt-get update sudo apt-get install chr ...