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 ...
随机推荐
- Java7的垃圾回收
HotSpot JVM一共有4个垃圾回收器:Serial(串行).Parallel / Throughput(并行).CMS(并发).and the new kid on the block G1(G ...
- 分析一个类似于jquery的小框架 (2)
核心构造函数 (function ( window, undefined ) { // 定义Itcast构造函数 function Itcast ( selector ) { return new I ...
- Parquet与ORC:高性能列式存储格式(收藏)
背景 随着大数据时代的到来,越来越多的数据流向了Hadoop生态圈,同时对于能够快速的从TB甚至PB级别的数据中获取有价值的数据对于一个产品和公司来说更加重要,在Hadoop生态圈的快速发展过程中,涌 ...
- Easyui表单之按钮的提交
一.表单按钮的提交前代表对提交内容的验证 二.表单按钮的提交后代表对把数据提交给后台 1. 界面层页面编辑代码: <!DOCTYPE html> <html> <head ...
- linux 学习10 shell 基础
10.1 Shell概述 .Shell是什么 Shell是一个命令行解释器,它为用户提供了一个向Linux内核发送请求以便运行程序的界面系统级程序,用户可以用Shell来启动.挂起.停止甚至是编写一 ...
- MediaBrowserService 音乐播放项目
MediaBrowserService 音乐播放项目,本项目主要有如下功能: (1):支持播放在线音乐 (2):按住home键退出页面后显示通知栏部分播放提示, (3) : 支持切换上下首歌曲 本项 ...
- cf 730i
题意:有n个人,每个人有两个能力值,选a个人用它的第一个能力值,b个人用它的第二个能力值,每个人只能选一次,求一个方案使得能力值之和最大,并输出选择方案. 题解:最小费用最大流,原点1向n个人每个人i ...
- 数据库一些常用的SQL语句
1.多表连接查询: 假设现在有三个表,One,Two,Three: One表字段:Code(主键),Name Two表字段:Birthday,T_code(One表Code的外键) Three表字段: ...
- table的遍历
1.for k,v in pairs (tbtest) do 这样的遍历顺序并不是tbtest中table的排列顺序,而是根据tbtest中key的hash值排列的顺序来遍历的 2.for k,v i ...
- 忘记BIOS超级管理员密码,怎么破解?
[请尊重原创版权,如需引用,请注明来源及地址] 本人就喜欢没事瞎折腾,动动手活动活动筋骨没坏处,前不久非常便宜的弄到一玩具 ThinkPad T400(公司处理品),外观还算不错,除了电源适配器是坏的 ...