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 ...
随机推荐
- php 微信 自定义分享接口
<?php class JSSDK { private $appId; private $appSecret; public function __construct($appId, $appS ...
- 在Linux中让文本显示带颜色的字。
在linux中让echo命令显示带颜色的字需要使用参数-e 格式:echo -e "\33[字体背景颜色:文字颜色m字符转\033[0m" 注: 1.字体背景颜色和文字颜色之间是英 ...
- Commix命令注入漏洞利用
介绍 项目地址:https://github.com/stasinopoulos/commix Commix是一个使用Python开发的漏洞测试工具,这个工具是为了方便的检测一个请求是否存在命令注入漏 ...
- CentOS6.4安装go环境
在官网上下载go1.6.linux-amd64.tar.gz 解压缩并拷贝程序到相应路径下 #tar -zxvf go1.6.linux-amd64.tar.gz #cp -rf go /usr/lo ...
- Implementing a builder: Combine
原文地址:点击这里 本篇我们继续讨论从一个使用Combine方法的computation expression中返回多值. 前面的故事 到现在为止,我们的表达式建造(builder)类如下 type ...
- 安卓---下拉刷新---上拉加载---解决导入library等自生成库文件失败的问题
本文的下拉刷新以及上拉加载都是用PullToRefresh实现的,关于PullToRefresh的介绍以及源码,网上可以找到很多,本人在此不再赘述. PullToRefresh是一套实现非常好的下拉刷 ...
- 更好列表页中一个航班.先unset删除数组中一个键值对,再追加,最后按键排序
<?php $arr = array( '0' => array('item' => array( 'aa' => 'aaa', 'bb' => 'bbb' )), '1 ...
- 设置div控件居中的方法
margin是设置外边距的,它有四个值,margin:4px 5px 5px 5px;分别表示上边距,右边距,下边距,和左边距,是从上按顺时针设置的,如果单个设置,他又有margin-top:3px: ...
- sql表命名规范
tb_new_Count 新闻内容表:首先表加tb_,如果是视图则加V_存储过程加_p. 下划线后面要大写. 切记.(萌萌的国庆要加油)
- Python之迭代器&装饰器&生成器&正则
1.迭代器 迭代器是访问数据集合的一种方式,它只能从集合的第一个元素开始顺序访问,直到最后一个元素结束.类似于linux里的cat命令,只能挨行读取文本内容,不可以跳到中间或者尾部读取(不会把所有的数 ...