getClass()。getClassLoader()。getResource()和getClass.getResource()之间的区别? [英] Difference between getClass().getClassLoader().getResource() and getClass.getResource()?

查看:259
本文介绍了getClass()。getClassLoader()。getResource()和getClass.getResource()之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从资源中检索文件时,我应该在什么情况下使用哪一个?

When retrieving files from resources, which one should I use in what circumstances?

推荐答案

第二个调用第一个。差异在javadoc中描述。

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.

所以 getClass()。getClassLoader()。getResource( foo / bar.txt)相当于 getClass()。getResource(/ foo / bar.txt)

并且,假设getClass()返回包 foo 中的类, getClass()。getResource(bar.txt)相当于 getClass()。getClassLoader()。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")

这篇关于getClass()。getClassLoader()。getResource()和getClass.getResource()之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆