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. win10开机时内存使用率达到99%以上

    开始,运行,输入msconfig回车就能看到自启的项目. 搞定! 其实,感觉特别像是输入法的某个监听程序导致内存泄漏,造成的系统问题. 再遇到的时候要认真检查下.

  2. Vue-router入门

    1.npm install vue-router --save-dev 安装路由包,在安装脚手架时实际上可以直接安装 2.解读核心文件 router/index.js文件 import Vue fro ...

  3. windows服务器剪贴板不能共用的解决办法

    远程桌面无法使用剪贴板共享纯文本的解决方法========================================以下操作须在远程桌面上操作,本地机没用的!================== ...

  4. oracle用户锁定

    https://www.cnblogs.com/lostyue/archive/2011/12/06/2278063.html

  5. Spring Cloud Feign 出现ClassNotFoundException: feign.Feign$Builder错误

    Spring Cloud Feign 出现ClassNotFoundException: feign.Feign$Builder错误 后来发现是POM文件写错了,修改为正确的pom,就可以了: POM ...

  6. UVA 11971 - Polygon 数学概率

                                        Polygon  John has been given a segment of lenght N, however he n ...

  7. bzoj1103: [POI2007]大都市meg(树链剖分)

    1103: [POI2007]大都市meg 题目:传送门 简要题意: 给你一棵树,给出每条边的权值,两个操作:1.询问根到编号x的最短路径的权值和  2.修改一条边的边权 题解: 很明显啊,看懂了题基 ...

  8. MongoDB数据查询详解

    查询全部 ​ db.infos.find(); db.infos.find({"url":"www.baidu.com"}); id不要显示出来 db.info ...

  9. [hihocoder #1384] Genius ACM 解题报告(倍增)

    题目链接:http://hihocoder.com/problemset/problem/1384 题目大意: 给定一个整数 M,对于任意一个整数集合 S,定义“校验值”如下: 从集合 S 中取出 M ...

  10. 关于如何让cell一直保持选中?

    在M上 1.cell的展示,一直都是依靠数据源的支持.所以,必须要在数据源里面新增bool,默认为false 在V上 2.cell的setModel方法里面,将数据源的新增bool赋值为cell的是否 ...