maven工程读取resource资源文件(踩到了这个坑)

本文介绍了在Maven项目中正确加载资源文件的方法,包括使用URL和InputStream的方式,并解释了类加载器(ClassLoader)的工作原理及常见陷阱。

网上有问答如下:
问:
  new FileInputStream(“src/main/resources/all.properties”)
  new FileInputStream(“./src/main/resources/all.properties”)
  上面两个无法读取maven下资源文件目录下的文件嘛,总是提示找不到该路径,这么写错了嘛,但是我的其他maven可以读取

答:
  要取编译后的路径,而不是你看到的src/main/resources的路径。如下:
  URL url = MyTest.class.getClassLoader().getResource(“conf.properties”);
  File file = new File(url.getFile());
  或者
  InputStream in = MyTest.class.getClassLoader().getResourceAsStream(“conf.properties”);

 

第二种方法,也可以改成
  InputStream in = getClass().getClassLoader().getResourceAsStream(“conf.properties”);

同事的一种的写法是:
  InputStream in = getClass().getResourceAsStream(“conf.properties”);

  然后怎么也获取不到,最后才发现,只要添加了.getClassLoader()就可以了


ClassLoader() 的作用


classLoader主要对类的请求提供服务,当JVM需要某类时,它根据名称向ClassLoader要求这个类,然后由ClassLoader返回这个类的class对象。

ClassLoader负责载入系统的所有资源(Class,文件,图片,来自网络的字节流等),通过ClassLoader从而将资源载入JVM 中。每个class都有一个引用,指向自己的ClassLoader。

getClassLoader() 的作用

  getClass():取得当前对象所属的Class对象 
  getClassLoader():取得该Class对象的类装载器
  类装载器负责从Java字符文件将字符流读入内存,并构造Class类对象,在你说的问题那里,通过它可以得到一个文件的输入

Class.getClassLoader()的一个小陷阱,空指针异常:
  昨天我的code总在Integer.class.getClassLoader().getResource(“*********”);这一句抛出空指针异常,定位为getClassLoader()返回null,查了一下jdk的文档,原来这里还有一个陷阱:
  这里jdk告诉我们:如果一个类是通过bootstrap 载入的,那我们通过这个类去获得classloader的话,有些jdk的实现是会返回一个null的,比如说我用 new Object().getClass().getClassLoader()的话,会返回一个null,这样的话上面的代码就会出现NullPointer异常.所以保险起见我们最好还是使用我们自己写的类来获取classloader(”this.getClass().getClassLoader()“),这样一来就不会有问题。

getResourceAsStream()方法详解

getResourceAsStream()用法与getResouce()方法一样的,用getResource()取得File文件后,再new FileInputStream(file) 与 getResourceAsStream() 的效果一样。。


给出示例 
两个代码效果一样

  1. InputStream inputStream1 = new FileInputStream(new File(Thread.currentThread().getContextClassLoader().getResource(”test.txt”).getFile()));
  2. //=============================
  3. InputStream inputStream2 = Thread.currentThread().getContextClassLoader().getResourceAsStream(”test.txt”);

参考:https://www.cnblogs.com/acm-bingzi/p/mavenResources.html

https://blog.youkuaiyun.com/my__sun_/article/details/74450241


            </div>
Maven 权威指南(中文) 非扫描版 如何在 windows 环境下配置Maven: 一、maven 在windows上的安装 1、下载apache-maven-3 的压缩包,并解压到 E:\developTools\apache-maven-3.1.1 2、配置环境变量 MAVEN_HOME=E:\developTools\apache-maven-3.1.1 在Path后添加 %MAVEN_HOME%\bin 3、验证是否安装成功 maven -version 4、配置maven的本地仓库 为<localRepository>E:\developTools\mavenRepository</localRepository> 二、使用命令创建一个maven 工程 1、创建一个简单的maven : mvn archetype:create -DgroupId=com.isoftstone.com.store -DartifactId=car -DpackgeName=com.isoftstone.product 2、创建一个maven的 web 项目: mvn archetype:create -DgroupId=com.isoftstone.com.store -DartifactId=car -DpackgeName=com.isoftstone.product -DarchetypeArtifactId=maven-archetype-webapp 3、在maven上集成jetty插件,这里使用bate 7.0版本 在<build>里面添加 <plugins> <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId>maven-jetty-plugin</artifactId> <version>7.0.0.pre5</version> </plugin> </plugins> 4、在tomcat 上集成tomcat插件 <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>tomcat-maven-plugin</artifactId> <version>1.1</version> <configuration> <path>/wp</path> <port>8080</port> <uriEncoding>UTF-8</uriEncoding> <url>http://localhost:8080/manager/html</url> <server>tomcat6</server> </configuration> </plugin> 使用 如果Eclipse 安装了Maven插件,选 择pom.xml文件,击右键——>选择 Run As——> Maven build。 三、在eclipse上安装maven插件 1、在线安装 略。 2、离线安装 下载maven的eclipse插件包 解压到eclipse安装目录的根目录下1、新建links和mavenPlugin文件夹 2、将插件包里的feature和plugin放到mavenPlugin里面3、在links里面新建maven.link文件,内容为path=mavenPlugin文件夹的目录 3、重起eclipse ,在preferences 下找到 Maven ,配置 installactions添加maven的解压路径 四、maven项目下 jar 引用的问题 1、从maven 自己的数据仓库上更新下载 jar 文件 2、手动将下载下来的 JAR 包添加到 Maven 的本地仓库,命令如下 mvn install:install-file -Dfile=jar包的位置 -DgroupId=上面的groupId -DartifactId=上面的artifactId -Dversion=上面的version -Dpackaging=jar
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值