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. nutch如何修改regex-urlfilter.txt爬取符合条件的链接

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

  2. HDU 1709

    MB,一开始就想到是不是只要加上一个不选择砝码的情况,但一直没动手做,因为看了看网上了,觉得总有点复杂,认为自己想错了.... 相信自己 #include <iostream> #incl ...

  3. 机器学习之&amp;&amp;Andrew Ng课程复习--- 聚类——Clustering

    第十三章.聚类--Clustering ******************************************************************************** ...

  4. MS UI Automation简介

    转自:http://blog.csdn.net/ffeiffei/article/details/6637418 MS UI Automation(Microsoft User Interface A ...

  5. 修改DNS

    解决方案一: 修改/etc/resolv.conf,添加 nameserver 8.8.8.8 nameserver 8.8.4.4 然后停用NetworkManager,service Networ ...

  6. [jzoj 5343] [NOIP2017模拟9.3A组] 健美猫 解题报告 (差分)

    题目链接: http://172.16.0.132/senior/#main/show/5343 题目: 题解: 记旋转i次之后的答案为$ans_i$,分别考虑每个元素对ans数组的贡献 若$s_i& ...

  7. Bringing up the Avnet MicroZed with Vivado

    Bringing up the Avnet MicroZed with Vivado I recently received the Adam Taylor Edition of Avnet's Zy ...

  8. System.IO.FileLoadException异常

    未能加载文件或程序集“NHibernate, Version=4.1.0.4000, Culture=neutral, PublicKeyToken=aa95f207798dfdb4”或它的某一个依赖 ...

  9. AndroidStudio EventBus报错解决方法its super classes have no public methods with the @Subscribe

    首先说明,以前我用eventBus的jar包写得项目demo,前几天就写了一个EventBus的实例,这次我没用jar包,直接用gradle引用的,可是demo写完了,报错: its super cl ...

  10. (转载)PopuWindow和软键盘共存时的设置

    PopuWindow和软键盘共存时的设置 收藏 artshell 发表于 2年前 阅读 1499 收藏 10 点赞 2 评论 0 腾讯云上实验室 1小时搭建人工智能应用 让技术更容易入门>> ...