classpath获取--getResource()
在java中的API里,有两种方式来使用classpath读取资源。
1. Class的getResource()
2. ClassLoader的getResource()
但是两者有一定区别,运行以下程序:
package zero.xml.config;
public class Main {
public static void main(String[] args) {
new Main().testGetResource();
}
public void testGetResource() {
System.out.println(Main.class.getResource("/").getPath());
System.out.println(Main.class.getResource("/app.properties").getPath());
System.out.println(Main.class.getResource("").getPath());
System.out.println(Main.class.getResource("app.properties").getPath());
System.out.println("-------------------");
System.out.println(this.getClass().getResource("/").getPath());
System.out.println(this.getClass().getResource("/app.properties").getPath());
System.out.println(this.getClass().getResource("").getPath());
System.out.println(this.getClass().getResource("app.properties").getPath());
System.out.println("-------------------");
System.out.println(Main.class.getClassLoader().getResource("").getPath());
System.out.println(Main.class.getClassLoader().getResource("app.properties").getPath());
System.out.println(Main.class.getClassLoader().getResource("zero/xml/config").getPath());
System.out.println(Main.class.getClassLoader().getResource("zero/xml/config/app.properties").getPath());
}
}
得到输出为:
/home/rain/git/spring-self-learn/bin/
/home/rain/git/spring-self-learn/bin/app.properties
/home/rain/git/spring-self-learn/bin/zero/xml/config/
/home/rain/git/spring-self-learn/bin/zero/xml/config/app.properties
-------------------
/home/rain/git/spring-self-learn/bin/
/home/rain/git/spring-self-learn/bin/app.properties
/home/rain/git/spring-self-learn/bin/zero/xml/config/
/home/rain/git/spring-self-learn/bin/zero/xml/config/app.properties
-------------------
/home/rain/git/spring-self-learn/bin/
/home/rain/git/spring-self-learn/bin/app.properties
/home/rain/git/spring-self-learn/bin/zero/xml/config
/home/rain/git/spring-self-learn/bin/zero/xml/config/app.properties
也就是:
1. 如果想获得classpath,使用以下方法:
System.out.println(Main.class.getResource("/").getPath());
System.out.println(Main.class.getClassLoader().getResource("").getPath());
2. 如果想获得classpath下的文件,使用以下方法:
System.out.println(Main.class.getResource("/app.properties").getPath());
System.out.println(Main.class.getClassLoader().getResource("app.properties").getPath());
3. 如果想获得当前类(比如zero.xml.config.Main)的路径,使用以下方法:
System.out.println(Main.class.getResource("").getPath());
System.out.println(Main.class.getClassLoader().getResource("zero/xml/config").getPath());
4. 如果想获得当前类路径下的文件,使用以下方法:
System.out.println(Main.class.getResource("app.properties").getPath());
System.out.println(Main.class.getClassLoader().getResource("zero/xml/config/app.properties").getPath());
注意,如果获取的文件或路径不存在,getResource()会返回null。比如,getClassLoader().getResource("/")就会返回null。
classpath获取--getResource()的更多相关文章
- classpath: VS classpath*:
同名资源存在时,classpath: 只从第一个符合条件的classpath中加载资源,而classpath*: 会从所有的classpath中加载符合条件的资源 classpath*:需要遍历所有的 ...
- classpath和filepath
******************************** java中的相对路径和绝对路径 ******************************** 相对路径(其实就是编译后的路径) - ...
- classpath: 和classpath*:的区别
classpath本质是jvm的根路径,jvm获取资源都是从该根路径下找的,注意这个根路径是个逻辑路径,并不是磁盘路径.比如两个jar包的路径是/a/a.jar和/b/b.jar,但是用classpa ...
- 通过SVN获取变更列表,得到对应的最新class
通过本地SVN获得未提交的文件列表获取工程中最新的class的方式参考: 增量部署代码利用批处理命令按原始结构复制指定的文件 新写了一个增强版,根据已提交至SVN的代码loglist,获取最新的cla ...
- [Java拾遗三]JavaWeb基础之Servlet
Servlet 1,servlet介绍 servlet是一项动态web资源开发技术. 运行在服务器端. 作用:处理业务逻辑,生成动态的内容,返回给浏览器 ...
- 上次遗留下来的XMLUtil的问题
·在上周留下了一个关于XMLUtil的问题,问题大概是这样的,需要通过读取一个XML文件,然后在内存中生成一个对应的javaBean.之前写的那个很是糟糕,照着一个XML去写了一个"Util ...
- 从源码角度深入分析log4j配置文件使用
log4j在日常开发中经常使用,但有时候对 配置文件应该放到什么位置有疑惑.现在我们通过从代码的角度来看待这个问题, 看完后你也许会恍然大悟哦. 开始吧. Log4j的组成及架构: Log4j由三个重 ...
- 从源码来理解slf4j的绑定,以及logback对配置文件的加载
项目中的日志系统使用的是slf4j + logback.slf4j作为一个简单日志门面,为各种loging APIs(像java.util.logging, logback, log4j)提供一个简单 ...
- spring资源访问接口和资源加载接口
spring 资源访问接口 JDK提供的资源访问类,如java.net.URL.File等,不能很好地满足各种资源的访问需求,比如缺少从类路径或者Web容器的上下文中获取资源的操作类. 鉴于此,spr ...
随机推荐
- STM32普通定时器(TIM2-7)的时钟源
STM32普通定时器(TIM2-7)的时钟源
- JavaScript DOM编程艺术-学习笔记(第十章、第十一章)
第十章 1.动画中,因为js的效率高,所以看不见过渡效果 2.题外话:①国外人写书,总是先感谢一遍亲朋好友,最后感谢自己的家人. 3."除非允许用户'冻结'移动的内容,否则应该避免让内容在页 ...
- MyBatis中update的使用
当你传入所需要修改的值为一个实体对象时,可能只改动了其中部分的值.那么其他值需要做一个判断是否为空值的操作. XXXmapper.xml <update id="updateMembe ...
- Spring对jdbc支持
4. Spring对jdbc支持 spring对jdbc提供了很好的支持 体现在: 1.Spring对C3P0连接池的支持很完善 2.Spring对jdbc提供了jdbcTemplate来简化jdbc ...
- 转:JMeter 参数化之利用JDBC Connection Configuration从数据库读取数据并关联变量
1. 下载mysql jar包 下载mysql jar包 http://dev.mysql.com/downloads/connector/j/ 网盘下载地址:mysql-connector-ja ...
- 部署到IIS后出现ORA-12560的解决办法
同事电脑IIS运行网站,出现ORA-12560错误,问了度娘半天都没问出个结果,最终通过bing找到了解决办法,如下: 1.按照常规方法将oracle client安装目录授权给network ser ...
- Javaweb 第15天 web练习和分页技术
第15天 web练习和分页技术 复习day14内容: 学习新技术的思路? 分析功能的思路? 使用queryRunner操作数据库的步骤? ResultSetHandler接口常用实现类(三个重点)? ...
- hadoop参数配置
Hadoop参数汇总 linux参数 JVM参数 Hadoop参数大全 core-default.xml hdfs-default.xml yarn-default.xml Hadoop参数汇总 @( ...
- 使用javassist运行时动态重新加载java类及其他替换选择
在不少的情况下,我们需要对生产中的系统进行问题排查,但是又不能重启应用,java应用不同于数据库的存储过程,至少到目前为止,还不能原生的支持随时进行编译替换,从这种角度来说,数据库比java的动态性要 ...
- 第一百零五节,JavaScript正则表达式
JavaScript正则表达式 学习要点: 1.什么是正则表达式 2.创建正则表达式 3.获取控制 4.常用的正则 假设用户需要在HTML表单中填写姓名.地址.出生日期等.那么在将表单提交到服务器进一 ...