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 ...
随机推荐
- 这是个简单的UTF8转码的小Demo
NSString *name = @"你好啊"; NSString *string = [NSString stringWithFormat:@"%@",nam ...
- linux 下 TeXmacs 作 Mathematica 10 的前端
TeXmacs可以作很多种数学软件的前端,比如maxima,octave,R等.甚至还可以作mathematica的前端.TeXmacs的mathematica 插件比较老,默认条件下无法运行math ...
- Nodejs学习(四)- express目录的分析
好久不来了,最近挺忙,就写一写下目录的情况吧. 我就说主要的目录,也就是我们经常用到的 public 用于存放一些js,css. routes 路由目录,如果你学过MVC应该不默生. views ...
- 关于js作用域链,以及闭包中的坑
eg:链式作用域,想在外部读取blogName的值得方法 <script>var authorName="山边小溪";function doSomething(){ ...
- 7.openssl enc
对称加密工具.了解对称加密的原理后就很简单了. [root@xuexi tmp]# man enc NAME enc - symmetric cipher routines SYNOPSIS open ...
- [Altera]PLL仿真
EDA Tools: 1.Quartus II 13.1(64-bit) 2.Modelsim SE-64 10.1c Time: 2016.05.05 ----------------------- ...
- RequireJS 基础(一)
RequireJS由James Burke创建,他也是AMD规范的创始人. RequireJS会让你以不同于往常的方式去写JavaScript,你将不再使用script标签在HTML中引入JS文件,以 ...
- 1-12 ARP协议
ARP(Address Resolution Protocol)地址解析协议,负责将相应的IP地址解析成MAC地址. 在局域网中,网络中实际传输的是‘帧’,帧里面包含了目的主机的MAC.ARP就是用来 ...
- 将sqlserve数据绑定到dataGridView中及一些操作
一:将数据绑定到dataGridView控件上. string sqlconn = "server=.;database=student;integrated security=true&q ...
- Spring Framework------>version4.3.5.RELAESE----->Reference Documentation学习心得----->Spring Framework的依赖注入和控制反转
Dependency Injection and Inversion of Control 1.概述: 1.1相关概念 bean:由IoC容器所管理的对象,也即各个类实例化所得对象都叫做bean 控制 ...