原文链接:http://shenchao.me/2016/04/20/maven%E4%B8%8B%E8%AF%BB%E5%8F%96%E8%B5%84%E6%BA%90%E6%96%87%E4%BB%B6%E7%9A%84%E9%97%AE%E9%A2%98/

新建一个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 两者的区别:

  1. 前者获取的是当前类加载的路径,如果用此方法读取文件则有两种方法,与相对路径绝对路径非常类似,具体参见代码
  2. 后者获取的是类加载器的路径,即会到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下读取资源文件的问题(转)的更多相关文章

  1. maven 编译部署src/main/java下的资源文件

    maven 编译部署src/main/java下的资源文件 maven默认会把src/main/resources下的所有配置文件以及src/main/java下的所有java文件打包或发布到targ ...

  2. 读取web应用下的资源文件(例如properties)

    package gz.itcast.b_resource; import java.io.IOException; import java.io.InputStream; import java.ut ...

  3. Maven学习-处理资源文件

    在前面两篇文章中,我们学习了Maven的基本使用方式和Maven项目的标准目录结构.接下来,我们来看下Maven是如果管理项目中的资源文件的. Java项目的资源文件,主要用于存储系统的配置信息,以及 ...

  4. Java/JavaWeb中读取资源文件

    1.一般工程中使用I/O类指定文件的绝对路径读取 FileInputStream fis = new FileInputStream("src/main/resources/zsm.prop ...

  5. (转)Maven学习-处理资源文件

    转自:http://www.cnblogs.com/now-fighting/p/4888343.html 在前面两篇文章中,我们学习了Maven的基本使用方式和Maven项目的标准目录结构.接下来, ...

  6. SpringBoot系列四:SpringBoot开发(改变环境属性、读取资源文件、Bean 配置、模版渲染、profile 配置)

    声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念 SpringBoot 开发深入 2.具体内容 在之前已经基本上了解了整个 SpringBoot 运行机制,但是也需要清 ...

  7. SpringBoot开发(改变环境属性、读取资源文件、Bean 配置、模版渲染、profile 配置)

    1.概念 SpringBoot 开发深入 2.具体内容 在之前已经基本上了解了整个 SpringBoot 运行机制,但是也需要清楚的认识到以下的问题,在实际的项目开发之中,尤其是 Java 的 MVC ...

  8. intelliJ idea读取资源文件

    原文:intelliJ idea读取资源文件 原文地址 http://yanwushu.sinaapp.com/intellij-idea_raed_resource_file/ 官方文档 以下是je ...

  9. J2EE之ServletContext读取资源文件

    ServletContext读取资源文件内容的方式有两种: 方法1. public void doGet(HttpServletRequest request, HttpServletResponse ...

随机推荐

  1. jsp中两种include的区别【转】

    引用文章:http://www.ibm.com/developerworks/cn/java/j-jsp04293/ http://www.cnblogs.com/lazycoding/archive ...

  2. boost.numpy编译报错:undefined reference to `PyInt_FromLong' libboost_numpy.so: undefined reference to `PyCObject_AsVoidPtr'

    [ 31%] Built target boost_numpy[ 36%] Building CXX object libs/numpy/example/CMakeFiles/dtype.dir/dt ...

  3. iOS图片加载到内存中占用内存情况

    我的测试结果: 图片占用内存   图片尺寸           .png文件大小 1MB              512*512          316KB 4MB              10 ...

  4. Nginx ssl证书部署

    查看当前安装的OpenSSL版本所支持的密码列表,可以使用下列命令:openssl ciphers 苹果ATS检测:https://www.qcloud.com/product/ssl 刚开始&quo ...

  5. sql join用法学习

    为了在两个或更多的表中获取结果,我们常常会用到join inner join(又叫join) out join包括left join,right join和full join(也就是left+righ ...

  6. Easyui表单之下拉列表的三级联动

    一.实现三级联动需要连接数据库 二.需要JSON数据的解析 三.需要Servlet类与界面相对应值的传递 1. 界面层需要的代码如下: <!DOCTYPE html> <html&g ...

  7. js计时事件

    通过在一个设定的时间间隔之后来执行代码,而不是在函数被调用后立即执行,我们称之为计时事件. 1. setTimeout()--暂停指定的时间后执行指定的代码 clearTimeout ()--停止se ...

  8. asp.net mvc return file result

    asp.net mvc返回文件: public ActionResult ExportReflection(string accessToken) { var reflections = GetCms ...

  9. Java:基于LinkedList实现栈和队列

    1.提供一组栈的接口,其底层关联到一个LinkedList(双端队列)实例.由于只暴露部分基于栈实现的接口,所以可以提供安全的栈实现. package junit; import java.util. ...

  10. TextVeiw 的 No package identifier when getting value for resource numb

    tv_title,tv_detail,tv_comment都是TextView; newInfo.getComment()得到的是int类型 tv_title.setText(newInfo.getT ...