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. 2016 10 28考试 dp 乱搞 树状数组

    2016 10 28 考试 时间 7:50 AM to 11:15 AM 下载链接: 试题 考试包 这次考试对自己的表现非常不满意!! T1看出来是dp题目,但是在考试过程中并没有推出转移方程,考虑了 ...

  2. nutch如何修改regex-urlfilter.txt爬取符合条件的链接

    例如我在爬取学生在线的时候,发现爬取不到特定的通知,例如<中粮福临门助学基金申请公告>,通过分析发现原来通知的链接被过滤掉了,下面对过滤url的配置文件regex-urlfilter.tx ...

  3. 创业笔记-Node.js入门之一个完整的基于Node.js的web应用

    用例 我们来把目标设定得简单点,不过也要够实际才行: 用户可以通过浏览器使用我们的应用. 当用户请求http://domain/start时,可以看到一个欢迎页面,页面上有一个文件上传的表单. 用户可 ...

  4. 洛谷——P3398 仓鼠找sugar

    https://www.luogu.org/problem/show?pid=3398#sub 题目描述 小仓鼠的和他的基(mei)友(zi)sugar住在地下洞穴中,每个节点的编号为1~n.地下洞穴 ...

  5. Java採用JNI调用VC++生成的dll(Java与C++交互)

    应项目需求,须要android调用java,java再调用C++实现android一个图片匹配的功能,我们作为java组须要和C++和Android进行交互.以下是java和C++採用JNI的方式进行 ...

  6. 数据挖掘算法学习(四)PCA算法

    转载请附上链接http://blog.csdn.net/iemyxie/article/details/38236647 算法简单介绍 主成分分析(PrincipalComponentAnalysis ...

  7. Android webView 缓存 Cache + HTML5离线功能 解决

    时间 -- :: CSDN博客 原文 http://blog.csdn.net/moubenmao/article/details/9076917 主题 Android HTML5 WebView的缓 ...

  8. 通过setInterval函数在地图上每隔1s打一次点

    <?php echo <<<_END <!doctype html> <html> <head> <meta charset=&quo ...

  9. Linux就该这么学 20181002(第二章基础命令)

    参考链接https://www.linuxprobe.com/ 忘记密码操作 启动页面 默认按e 在linux16行后空格 rd.break ctrl + x mount -o remount,rw ...

  10. windows模式编译

    //预编译,linker链接,Windows模式#pragma comment(linker,"/subsystem:\"windows\" /entry:\" ...