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 ...
随机推荐
- Linux常用命令1
jps(Java Virtual Machine Process Status Tool)是JDK 1.5提供的一个显示当前所有Java进程pid的命令. jps [ options ] [ host ...
- python3.5------三级菜单
笔者QQ :360212316 逻辑图 代码 city = { "华南": { "广东": ["广州市", "佛山市", ...
- PHP抓取及分析网页的方法详解
本文实例讲述了PHP抓取及分析网页的方法.分享给大家供大家参考,具体如下: 抓取和分析一个文件是非常简单的事.这个教程将通过一个例子带领你一步一步地去实现它.让我们开始吧! 首先,我首必须决定我们将抓 ...
- 正则表达式提取string 中的表名
简单版本: Regex reg = new Regex(@"(?i)\bfrom\b(?![^\[\]]*\])\s+(\[[^\[\]]+\]|\S+)"); MatchColl ...
- windows下手动安装和配置xamarin
安装xamarin xamarin官方给出了两种安装方式,自动安装和手动安装. 自动安装比较简单,到http://xamarin.com/download下载xamarininstaller.exe ...
- 犀利点评:csdn某文<第一次创业还是失败了---分享失败的经验>
今天上午在csdn看了一篇创业文,突然想无节操的做一下点评. 原文详细地址如下:http://blog.csdn.net/android_tutor/article/details/9815801 以 ...
- Qt界面对象的事件调用
QMetaObject::invokeMethod(m_mainToolBarItem, "change2DesktopMode", Q_ARG(QVariant, m_curMo ...
- hdu1540 Tunnel Warfare
Tunnel Warfare Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- python批量处理excel文件数据
https://www.zhihu.com/question/39299070?sort=created 作者:水中柳影链接:https://www.zhihu.com/question/392990 ...
- VS 2008 快捷键
注释代码:<Ctrl+K,C>取消注释:<Ctrl+K,U> 封装字段(生成get.set方法): <Ctrl+R,E> 定位大括号范围:光标放在其中一个括号的位置 ...