参考博客: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. try catch finally return

    public override bool Start(IServerConfig config) { bool flag = true; listenSocket = new Socket(Liste ...

  2. C++ 自定义错误类

    #include <iostream> #include <exception> using namespace std; struct MyException : publi ...

  3. jQuery实现鼠标点击Div区域外隐藏Div

    冒泡定义:当一个元素上的事件被触发的时候,比如说鼠标点击了一个按钮,同样的事件将会在那个元素的所有祖先元素中被触发.这一过程被称为事件冒泡:这个事件从原始元素开始一直冒泡到DOM树的最上层.(摘自网络 ...

  4. POJ 2486 Apple Tree(树形dp)

    http://poj.org/problem?id=2486 题意: 有n个点,每个点有一个权值,从1出发,走k步,最多能获得多少权值.(每个点只能获得一次) 思路: 从1点开始,往下dfs,对于每个 ...

  5. 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 ...

  6. Qt532_QWebView做成DLL供VC/Delphi使用_Bug

    Qt5.3.2 vs2010 OpenGL ,VC6.0,Delphi7 1.自己继承 类QWebView,制作成DLL 供 VC6/Delphi7 使用 2.测试下来,DLL供VC6使用: 加载&q ...

  7. js 面试题总结

    面试题解析 window.number = 1; var obj = { number: 4, dbl: (function(){ console.log(obj.number); this.numb ...

  8. HIVE之常用字符串函数

    可以参考: 博文 : https://www.iteblog.com/archives/1639.html

  9. openv+contrib配置总结

    本文转载于:https://www.cnblogs.com/wjy-lulu/p/6805557.html 开门见山的说:别用opencv3.0,这个版本添加扩展库不怎么好,能不能成功我不敢说,我是试 ...

  10. Leetcode 16

    //一次AC 有点爽的class Solution { public: int threeSumClosest(vector<int>& nums, int target) { ; ...