getClassLoader().getResource()和getResource()的区别

eg:
TemplateCode.class.getClass().getClassLoader().getResource("foo/bar.txt")

TemplateCode.class.getClassLoader().getResource("foo/bar.txt")的区别

第二句话实际上还是调用的第一句,
区别在于第一句话不能以/开始,并总是从classpath的根目录开始查找,
第二句如果以/开始,则从根目录开始查找,如果开始不加/则从当前类所在的路径开始查找

[b][color=red]验证通过[/color][/b]

参考原文

http://stackoverflow.com/questions/14739550/what-is-the-difference-between-getclass-getclassloader-getresource-and-get

[quote]The second one calls the first one. The difference is described in the javadoc.

The first one takes paths that don't start with a /, and always starts at the root of the classpath.

The second one takes path that can start with a /. If it does, it starts at the root of the classpath. If not, it starts at the package of the class on which the method is called.

So getClass().getClassLoader().getResource("foo/bar.txt") is equivalent to getClass().getResource("/foo/bar.txt").

And, assuming getClass() returns a class that is in the package foo, getClass().getResource("bar.txt") is equivalent to getClass().getClassLoader().getResource("foo/bar.txt")[/quote]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值