`
roc08
  • 浏览: 224615 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

    博客分类:
  • java
阅读更多

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

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

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

验证通过

参考原文

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

引用
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")
分享到:
评论

相关推荐

    Java路径问题解决方案汇集

     URLTest.class.getClassLoader().getResource().getPath();  Thread.currentThread().getContextClassLoader().getResource().getPath();等多种相似方式获得的路径,不能被FileReader()和FileWriter()直接应用...

    sax解析util

    URL url = Configuration.class.getClassLoader().getResource(filename); String str = url.getFile(); // 转换编码 str = URLDecoder.decode(str, "utf-8"); File conf = new File(str); ...

    Digester解析xml

    Digester digester = DigesterLoader.createDigester(TestDigester.class.getClassLoader().getResource(Constants.CONFIG_MAPPINGRULE_PATH)); digester.setValidating(false);

    经典java小程序源代码合集.zip

    Image white= tk.getImage(cl.getResource("res/white.png")); Image black= tk.getImage(cl.getResource("res/black.png")); Image title= tk.getImage(cl.getResource("res/title.png")); Image temp; ...

    java自定义弹窗Demo

    Icon icon = new ImageIcon(ConfirmDialog.class.getClassLoader().getResource("info.png")); ScaleIcon scaleIcon = new ScaleIcon(icon); JLabel iconLabel = new JLabel(scaleIcon); iconLabel....

    应用移植WebSphere问题列表(经验是买不到的)

    在websphere通过getClassLoader().getResource("").getPath()取配置文件的路径跟在tomcat环境下是不一样的,如果配置文件是放在classes目录下,会报找不到配置文件。 发布应用时,报web.xml文件有错 发布应用成功,...

    properties资源文件utils类

    String filepath = ContentMessage.class.getClassLoader().getResource("") .getPath(); filepath = filepath.substring(0, filepath.length() - 8); filepath = filepath + "resource/Application...

    Java获取*路径实现探讨

    (1)、request.getRealPath(“/”);//不推荐使用获取工程的根路径 (2)、...//获取工程的根路径,这个方法比较好用,可以直接在servlet和jsp中使用 (4)、 this.getClass().getClassLoader().getResource(“”).getPath();

    testng-parser:TestNG解析器

    testng-parser:一个简单的TestNG解析器TestNG Parser项目带有一个...URL url = cl.getResource("com/tupilabs/testng/parser/testng-results.xml");File file = new File(url.getFile());List<Suite> suites

    Java开发的小游戏 仿坦克大战

    im=tk.getImage(Tree.class.getClassLoader().getResource("images/dor.gif")); } public void draw(Graphics g) { if(!live) return; g.drawImage(im, x, y, null); if(r.nextInt(40) > 38){ //...

    dom4j-jdom封装和解析例子

    String fromRead=Dom4jTest2.class.getClassLoader().getResource("xml/map1.xml").getPath(); List<Pois> list=new ArrayList(); SAXReader saxReader = new SAXReader(); org.dom4j.Document document=...

    java五子棋源码

    Image white= tk.getImage(cl.getResource("res/white.png")); Image black= tk.getImage(cl.getResource("res/black.png")); Image title= tk.getImage(cl.getResource("res/title.png")); Image temp; JPanel...

    Socket聊天室源程序

    Object obj = e.getSource(); if (obj == userItem || obj == userButton) { //用户信息设置 //调出用户信息设置对话框 UserConf userConf = new UserConf(this,userName); userConf.show(); ...

    ViewPager 放大缩小左右移动

    final ViewConfiguration configuration = ViewConfiguration.get(context); mTouchSlop = ViewConfigurationCompat .getScaledPagingTouchSlop(configuration); mMinimumVelocity = configuration....

Global site tag (gtag.js) - Google Analytics