参考博客:http://www.cnblogs.com/yejg1212/p/3270152.html

Class.getResource(String path)

当path以/开头,如/a/b/c.properties时,查找的路径为: classpath路径:path。
当path不以/开头时,如c.properties,查找的路径为:当前调用类的路径:path。

什么意思呢?看下面这段代码的输出结果就明白了:

package testpackage;
public class TestMain {
public static void main(String[] args) {
System.out.println(TestMain.class.getResource(""));
System.out.println(TestMain.class.getResource("/"));
}
} 输出结果:
file:/E:/workspace/Test/bin/testpackage/
file:/E:/workspace/Test/bin/

※Class.getResource和Class.getResourceAsStream在使用时,路径选择上是一样的。

Class.getClassLoader().getResource(String path)

path不能以’/'开头时;
path是从ClassPath根下获取;
package testpackage;
public class TestMain {
public static void main(String[] args) {
TestMain t = new TestMain();
System.out.println(t.getClass());
System.out.println(t.getClass().getClassLoader());
System.out.println(t.getClass().getClassLoader().getResource(""));
System.out.println(t.getClass().getClassLoader().getResource("/"));//null
}
} //输出结果:
class testpackage.TestMain
sun.misc.Launcher$AppClassLoader@1fb8ee3
file:/E:/workspace/Test/bin/
null

※Class.getClassLoader().getResource和Class.getClassLoader().getResourceAsStream在使用时,路径选择上也是一样的。

但是我做的实验下面:getClassLoader().getResource("/")也是行的,为什么呢?

在TestController中想要获取com/cy/config包下面的config.xml文件:

@Controller
public class TestController { private static Logger logger = (Logger) LoggerFactory.getLogger(TestController.class); @RequestMapping("/test")
public void test(HttpServletRequest request){
String path = this.getClass().getResource("/").getPath() + "com"+File.separator+"cy"+File.separator+"config"+File.separator+"config.xml";
System.out.println(path);
// /D:/tomcat8.5.9/webapps/springMVC/WEB-INF/classes/com\cy\config\config.xml String path2 = this.getClass().getResource("").getPath() + "com"+File.separator+"cy"+File.separator+"config"+File.separator+"config.xml";
System.out.println(path2);
// /D:/tomcat8.5.9/webapps/springMVC/WEB-INF/classes/com/cy/controller/com\cy\config\config.xml String path3 = this.getClass().getClassLoader().getResource("/").getPath() + "com"+File.separator+"cy"+File.separator+"config"+File.separator+"config.xml";
System.out.println(path3);
// /D:/tomcat8.5.9/webapps/springMVC/WEB-INF/classes/com\cy\config\config.xml String path4 = this.getClass().getClassLoader().getResource("").getPath() + "com"+File.separator+"cy"+File.separator+"config"+File.separator+"config.xml";
System.out.println(path4);
// /D:/tomcat8.5.9/webapps/springMVC/WEB-INF/classes/com\cy\config\config.xml
}
}

注意的是:

1.                                        

this.getClass().getClassLoader().getResource("").getPath()中如果文件的路径中有#空格等特殊字符的话,会转义为%23等这样的字符,得到的路径就不正确

这时候要将特殊字符转义,这是我在项目中遇到的:

URLDecoder.decode(path, "UTF-8")

2.File.separator:                                        

File.separator: 是系统分隔符,比如windows下面文件路径的分隔符是"\"; linux下面是"/"

但是在windows下面和linux下面都可以用"/"

												

关于Class.getResource和ClassLoader.getResource的路径问题(转)的更多相关文章

  1. 关于Class.getResource和ClassLoader.getResource的路径问题

    Java中取资源时,经常用到Class.getResource和ClassLoader.getResource,这里来看看他们在取资源文件时候的路径问题. Class.getResource(Stri ...

  2. 【转】关于Class.getResource和ClassLoader.getResource的路径问题

    Java中取资源时,经常用到Class.getResource和ClassLoader.getResource,这里来看看他们在取资源文件时候的路径问题. Class.getResource(Stri ...

  3. (转)关于Class.getResource和ClassLoader.getResource的路径问题

    Java中取资源时,经常用到Class.getResource和ClassLoader.getResource,这里来看看他们在取资源文件时候的路径问题. 1 Class.getResource(St ...

  4. Class.getResource和ClassLoader.getResource的路径写法

    Java中取资源时,经常用到Class.getResource和ClassLoader.getResource,这里来看看他们在取资源文件时候的路径问题. Class.getResource(Stri ...

  5. Class.getResource和ClassLoader.getResource的区别分析

    原文:http://swiftlet.net/archives/868 在Java中获取资源的时候,经常用到Class.getResource和ClassLoader.getResource,本文给大 ...

  6. 关于获取资源文件,Class.getResource和ClassLoader.getResource的区别

    原文同步发表至个人博客[夜月归途] 原文链接:http://www.guitu18.com/se/java/2019-02-22/29.html 作者:夜月归途 出处:http://www.guitu ...

  7. 【JAVA】Class.getResource()与ClassLoader.getResource()的区别

    转载自:https://blog.csdn.net/qq_33591903/article/details/91444342 Class.getResource()与ClassLoader.getRe ...

  8. 使用Class.getResource和ClassLoader.getResource方法获取文件路径

    自从转投Java阵营后,一直发下Java程序的路径读取异常麻烦,因此查阅了比较多的版本内容,整合了一份自己的学习笔记.主要使用Class及通过ClassLoader来动态获取文件路径. 查阅链接如下: ...

  9. 【转载】使用Class.getResource和ClassLoader.getResource方法获取文件路径

    自从转投Java阵营后,一直发下Java程序的路径读取异常麻烦,因此查阅了比较多的版本内容,整合了一份自己的学习笔记.主要使用Class及通过ClassLoader来动态获取文件路径. 查阅链接如下: ...

  10. java Class.getResource和ClassLoader.getResource

    http://www.cnblogs.com/wang-meng/p/5574071.html http://blog.csdn.net/earbao/article/details/50009241 ...

随机推荐

  1. Windows自带计算器快捷键

    今天乱翻的时候发现了这个东西,下面就是各个快捷键: (以下功能在计算器面板上均能找到) 按键 功能 F9 \(-/+\) R 1/x @ \(\sqrt{}\) Ctrl+Shift+D 清除历史记录 ...

  2. R语言笔记-set.seed()函数

    今天查了一下R语言中set.seed(),该命令的作用是设定生成随机数的种子,种子是为了让结果具有重复性.如果不设定种子,生成的随机数无法重现. set.seed()用于设定随机数种子,一个特定的种子 ...

  3. Codeforces Round #319 (Div. 2) C. Vasya and Petya's Game 数学

    C. Vasya and Petya's Game time limit per test 1 second memory limit per test 256 megabytes input sta ...

  4. Qt5编译error01

    1.Qt551x86 vs2013 1.1.“error: C2001: 常量中有换行符” 问题还原:源码:“QString str = tr("已接收 %1MB (%2MB/s) \n共% ...

  5. Linux CentOS 6.5 使用自带jdk修改环境变量

    首先声明,默认jdk指我们安装完CentOS后系统自带jdk,自己下载安装的jdk只需要下载,解压即可,之后步骤与此文一致 1.查看我们默认jdk的位置 指令: which java 我们去看一下发现 ...

  6. POJ3279 搜索/思维

    Fliptile Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10457   Accepted: 3866 Descrip ...

  7. Mybatis整合Spring -- typeAliasesPackage

    Mybatis整合Spring 根据官方的说法,在ibatis3,也就是Mybatis3问世之前,Spring3的开发工作就已经完成了,所以Spring3中还是没有对Mybatis3的支持. 因此由M ...

  8. 005PHP文件处理——目录操作,统计大小 filesize unlink

    <?php /* 目录操作,统计大小 filesize unlink * */ $dir = dir("."); while (($file = $dir->read( ...

  9. 008——php字符串中的处理函数(七)

    <?php /** *字符串处理函数(六)get_magic_quotes_runtime set_magic_quotes_runtime strip_tags *get_magic_quot ...

  10. ViewPager + Fragment 实现主界面底部导航栏

    1. 四个类似的Frament布局 tab_main_fragment.xml <LinearLayout xmlns:android="http://schemas.android. ...