maven下读取资源文件的问题(转)
新建一个maven工程后,main目录下会有java和resources两个文件夹,其中java文件夹下存放源代码,resources文件夹下存放一些配置文件等。

在弄清楚编译后,资源文件以及字节码存在哪里这个问题之前,有必要明白什么是classpath
classpath实际上就是编译后的 以 classes 文件夹为起点的路径,而在ItelliJ IDEA 中编译后的文件都会存入target/classes下。
所以,编译后,resources文件夹中的文件以及java目录下的文件都会存入同一个目录(target/classes)下,也就是说,编译后是不存在java和resources这两个目录的。
读取资源文件的若干中方法
package me.songfeng.main; import java.io.BufferedReader;
import java.io.FileReader; /**
* Created by songfeng on 16/10/15.
*/
public class Demo1 {
private static void readTxt(String filePath){
BufferedReader reader =null;
try{
reader = new BufferedReader(new FileReader(filePath));
String line = null;
while((line = reader.readLine())!= null){
System.out.println(line);
}
}catch (Exception e){
e.printStackTrace();
}finally {
if(reader != null){
try{
reader.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
} public static void main(String[] args) {
// 获取classpath路径
System.out.println("classpath路径: " + Demo1.class.getClassLoader().getResource("").getPath()); // 获取当前类的加载路径
System.out.println("当前类加载路径: " + Demo1.class.getResource("").getPath()); readTxt(Demo1.class.getClassLoader().getResource("test/demo1.txt").getPath()); readTxt(Demo1.class.getResource("/test/demo1.txt").getPath()); readTxt(Demo1.class.getResource("../../../test/demo1.txt").getPath());
} }
其中,demo1.txt文件中的内容为:
hahahaha
输出如下:
classpath路径: /Users/songfeng/Work/mavendemo/target/classes/
当前类加载路径: /Users/songfeng/Work/mavendemo/target/classes/me/songfeng/main/
hahahaha
hahahaha
hahahaha Process finished with exit code 0
从上面可以发现getResource 与 class.getClassLoader().getResource 两者的区别:
- 前者获取的是当前类加载的路径,如果用此方法读取文件则有两种方法,与相对路径绝对路径非常类似,具体参见代码
- 后者获取的是类加载器的路径,即会到classpath路径下。可以理解当前在 classp/ 目录下,要想访问哪个文件,直接填写路径即可,不用区分相对路径和绝对路径。显然,此方法比较容易写出。推荐。
读取配置文件,可以参照下面这种方式:
/**
* Find, read, and parse the configuration file.
* @return the properties that were found or empty if no file was found
*/
private static Properties readConfigFile() {
Properties properties = new Properties(); //Get property file stream from classpath
InputStream inputStream = Configuration.class.getClassLoader().getResourceAsStream(CONFIG_FILE); if (inputStream == null) {
throw new RuntimeException(CONFIG_FILE + " not found in classpath");
} // load the properties
try {
properties.load(inputStream);
inputStream.close();
} catch (FileNotFoundException fnf) {
LOG.info("No configuration file " + CONFIG_FILE + " found in classpath.", fnf);
} catch (IOException ie) {
throw new IllegalArgumentException("Can't read configuration file " +
CONFIG_FILE, ie);
} return properties;
}
maven下读取资源文件的问题(转)的更多相关文章
- maven 编译部署src/main/java下的资源文件
maven 编译部署src/main/java下的资源文件 maven默认会把src/main/resources下的所有配置文件以及src/main/java下的所有java文件打包或发布到targ ...
- 读取web应用下的资源文件(例如properties)
package gz.itcast.b_resource; import java.io.IOException; import java.io.InputStream; import java.ut ...
- Maven学习-处理资源文件
在前面两篇文章中,我们学习了Maven的基本使用方式和Maven项目的标准目录结构.接下来,我们来看下Maven是如果管理项目中的资源文件的. Java项目的资源文件,主要用于存储系统的配置信息,以及 ...
- Java/JavaWeb中读取资源文件
1.一般工程中使用I/O类指定文件的绝对路径读取 FileInputStream fis = new FileInputStream("src/main/resources/zsm.prop ...
- (转)Maven学习-处理资源文件
转自:http://www.cnblogs.com/now-fighting/p/4888343.html 在前面两篇文章中,我们学习了Maven的基本使用方式和Maven项目的标准目录结构.接下来, ...
- SpringBoot系列四:SpringBoot开发(改变环境属性、读取资源文件、Bean 配置、模版渲染、profile 配置)
声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念 SpringBoot 开发深入 2.具体内容 在之前已经基本上了解了整个 SpringBoot 运行机制,但是也需要清 ...
- SpringBoot开发(改变环境属性、读取资源文件、Bean 配置、模版渲染、profile 配置)
1.概念 SpringBoot 开发深入 2.具体内容 在之前已经基本上了解了整个 SpringBoot 运行机制,但是也需要清楚的认识到以下的问题,在实际的项目开发之中,尤其是 Java 的 MVC ...
- intelliJ idea读取资源文件
原文:intelliJ idea读取资源文件 原文地址 http://yanwushu.sinaapp.com/intellij-idea_raed_resource_file/ 官方文档 以下是je ...
- J2EE之ServletContext读取资源文件
ServletContext读取资源文件内容的方式有两种: 方法1. public void doGet(HttpServletRequest request, HttpServletResponse ...
随机推荐
- iOS7——UIControlEventTouchDown延迟响应问题
问题描述 在iOS7下开发,真机调试时,UIButton的其他事件响应都正常,但是UIControlEventTouchDown事件响应会延迟,而且不同响应区域发生的延时情况不同,有时延迟1s以后响应 ...
- C# 异常处理 <-> 连接远程数据库遇到的问题
PS : 移植类库需要重新生成解决方案:(要不然不能将类库添加进项目)
- python学习心得第五章
python学习心得第五章 1.冒泡排序: 冒泡是一种基础的算法,通过这算法可以将一堆值进行有效的排列,可以是从大到小,可以从小到大,条件是任意给出的. 冒泡的原理: 将需要比较的数(n个)有序的两个 ...
- 【转】Web前端研发工程师编程能力飞升之路
分类: Javascript | 出自 海玉的博客 今天看到这篇文章.写的非常有意思.发现自己还有很长的一段路要走. [背景] 如果你是刚进入WEB前端研发领域,想试试这潭水有多深,看这篇文章吧: 如 ...
- .net 下载文件几种方式
方式一:TransmitFile实现下载.将指定的文件直接写入 HTTP 响应输出流,而不在内存中缓冲该文件. protected void Button1_Click(object sender, ...
- Linux操作系统下搭建LAMP环境
准备:先在目录home/csy/下建website代码目录,然后新建php文件,命名为test.php. 在test.php编写代码如下: <? php phpinfo(); ?> 保存并 ...
- Markdown 快速入门
使用Markdown编辑器:MarkdownPad 2 标题: # 标题 ## 标题 ### 标题 #### 标题 ##### 标题 ###### 标题 效果: 标题 标题 标题 标题 标题 标题 下 ...
- webservice 接口通过 HTTP 获取数据
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Ne ...
- 修改Shp文件名称
IWorkspaceFactory factory = new ShapefileWorkspaceFactoryClass(); IWorkspace pworkspace = factory.Op ...
- exception 'yii\base\ErrorException' with message 'Class 'MongoClient' not found'
问题描述: 本来项目运行的好好的,搬了一次办公室(电脑主机一起搬的),第二天的时候就登录不了了. php版本和扩展没有改变,且没有修改任何配置,我尝试重启php5-fpm 服务,又重启nginx服务, ...