java 如何读取src根目录下的属性文件
在java项目中,如何获取src根目录下的属性文件/资源文件呢?
有如下三种方式:
方式一:
InputStream in = Test.class
.getResourceAsStream("/env.properties");
URL url = Test.class.getResource("<span style="color: #000000;">/</span>env.properties") ;
说明:env.properties文件在src的根目录下,文件名前有斜杠
方式二:
InputStream in = Test.class.getClassLoader()
.getResourceAsStream("env.properties");
URL url = Test.class.getClassLoader().getResource("env.properties") ;
方式三:
InputStream in = Thread.currentThread().getContextClassLoader()
.getResourceAsStream("ExcelModeMappingl.xml");
示例:
private Properties readConfig(){
InputStream in = this.getClass().getResourceAsStream("/config.properties");
// String path = this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
// int lastIndex = path.lastIndexOf(File.separator) + 1;
// path = path.substring(0, lastIndex);
// File configFile = new File(path + "configration.properties");
// InputStream in = null;
// try
// {
// in = new FileInputStream(configFile);
// }
// catch (FileNotFoundException e)
// {
// e.printStackTrace();
// }
Properties props = new Properties();
InputStreamReader inputStreamReader = null;
try
{
inputStreamReader = new InputStreamReader(in, "UTF-8");
props.load(inputStreamReader);
}
catch (Exception e)
{
e.printStackTrace();
}
return props;
}
config.properties在src/main/resources目录下
注释掉的部分是读取jar包平级目录下的配置文件。有时需要把配置文件放在jar包外面方便配置,因此需要读取jar包外的配置文件。
本地读取资源文件
java类中需要读取properties中的配置文件,可以采用文件(File)方式进行读取:
1 File file = new File("src/main/resources/properties/basecom.properties");
2 InputStream in = new FileInputStream(file);
当在eclipse中运行(不部署到服务器上),可以读取到文件。
服务器(Tomcat)读取资源文件
采用流+Properties
当工程部署到Tomcat中时,按照上边方式,则会出现找不到该文件路径的异常。经搜索资料知道,Java工程打包部署到Tomcat中时,properties的路径变到顶层(classes下),这是由Maven工程结构决定的。由Maven构建的web工程,主代码放在src/main/java路径下,资源放在src/main/resources路径下,当构建为war包的时候,会将主代码和资源文件放置classes文件夹下:

并且,此时读取文件需要采用流(stream)的方式读取,并通过JDK中Properties类加载,可以方便的获取到配置文件中的信息,如下:
InputStream in = this.getClass().getResourceAsStream("/properties/basecom.properties");
Properties properties = new Properties();
properties.load(in);
properties.getProperty("property_name");
其中properties前的斜杠,相对于调用类,共同的顶层路径。
java 如何读取src根目录下的属性文件的更多相关文章
- Spark:java api读取hdfs目录下多个文件
需求: 由于一个大文件,在spark中加载性能比较差.于是把一个大文件拆分为多个小文件后上传到hdfs,然而在spark2.2下如何加载某个目录下多个文件呢? public class SparkJo ...
- Java中读取某个目录下的所有文件和文件夹
import java.io.File; public class Test1 { public static void main(String[] args) { String path=" ...
- 【转】忙里偷闲写的小例子---读取android根目录下的文件或文件夹
原文网址:http://www.cnblogs.com/wenjiang/p/3140055.html 最近几天真的是各种意义上的忙,忙着考试,还要忙着课程设计,手上又有外包的项目,另一边学校的项目还 ...
- Android 读取手机SD卡根目录下某个txt文件的文件内容
1.先看activity_main.xml文件: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/and ...
- 忙里偷闲写的小例子---读取android根目录下的文件或文件夹
最近几天真的是各种意义上的忙,忙着考试,还要忙着课程设计,手上又有外包的项目,另一边学校的项目还要搞,自己的东西还在文档阶段,真的是让人想死啊!! 近半个月来,C#这方面的编码比较多,android和 ...
- java通过文件路径读取该路径下的所有文件并将其放入list中
java通过文件路径读取该路径下的所有文件并将其放入list中 java中可以通过递归的方式获取指定路径下的所有文件并将其放入List集合中.假设指定路径为path,目标集合为fileList,遍 ...
- 【转】读取android根目录下的文件或文件夹
原文网址:http://my.oschina.net/Ccx371161810/blog/287823 读取android根目录下的文件或文件夹 SDK的操作 读取android根目录下的文件或文件夹 ...
- 读取web应用下的资源文件(例如properties)
package gz.itcast.b_resource; import java.io.IOException; import java.io.InputStream; import java.ut ...
- [转]读取assets目录下的数据库文件
在做Android应用的时候,不可避免要用到数据库.但是当我们把应用的apk部署到真机上的时候,已经创建好的数据库及其里边的数据是不能随着apk一起安装到真机上的. (PS:这篇博客解决了我前面博客中 ...
随机推荐
- python基础学习19,01
听说python入门很容易,所以想试试对我这个零基础会不会很友好呢.做随笔也顺便督促自己不要轻易放弃学习. #登录接口 print("请输入用户名密码") _username = ...
- SecureCRT 8.3注册码下载
SecureCRT注册码是一款针对“SecureCRT 8.3”软件而制作的激活破解工具,可以完美激活“SecureCRT”软件,从而达到免费使用的目的.而SecureCRT则是一款非常优秀的且支持s ...
- C#对IQueryable<T>、IEnumerable<T>的扩展方法
#region IQueryable<T>的扩展方法 #region 根据第三方条件是否为真是否执行指定条件的查询 /// <summary> /// 根据第三方条件是否为真是 ...
- kubespray 修改配置
1.安装前的修改配置 # [root@slave1 kubespray]# vim inventory/local/group_vars/k8s-cluster.yml kube_network_pl ...
- linux系统下安装python3及其配置
Linux下安装Python3.6和第三方库 linux一般自带python2,不要动它,使用python3运行python脚本就好,部分linux系统命令依赖目前的python2环境, 比如yum! ...
- Design Hit Counter
Design a hit counter which counts the number of hits received in the past 5 minutes. Each function a ...
- Docker 运行 MYSQL 数据库的总结
公司里面要求做一个小demo 学习java相关的东西 然后使用了mysql数据库 很早之前做过mysql的容器化运行. 现在想想已经忘记的差不多了 所以这里总结一下 docker化运行mysql数据 ...
- python -- TypeError: 'module' object is not callable
文件: 代码: import pprintmessge = 'It was a bringht cold day in April,and the clocks were striking thrir ...
- Zero Array---思维题
链接 submit B. Zero Array time limit per test 1 second memory limit per test 256 megabytes inpu ...
- MySQL之主键
一.主键 primary key (唯一标识 .不能重复.不能为空) 1.主键-----是表中的字段,这个字段能唯一标识一条记录.例如 学生表(学号.姓名,年级)里的学号,不能重复.不能为空: 课程 ...