getClassLoader()。getResourceAsStream()如何在java中工作 [英] How getClassLoader().getResourceAsStream() works in java

查看:111
本文介绍了getClassLoader()。getResourceAsStream()如何在java中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我谷歌如何在下面的代码加载资源
Abc.class.getClassLoader()。getResourceAsStream(abc.txt)

并发现它搜索所有jar文件中的资源类路径中的zip文件。

I google how below code loads the resource Abc.class.getClassLoader().getResourceAsStream("abc.txt")
and find that it searchs the resource in all jar file and zip file in class path.

但是当我尝试它时我无法加载它但是如果我提供包路径然后我可以加载它
谁能告诉我getResourceAsStream如何搜索类路径

But when i tried it I am not able to loads it but if i give package path then I am able to loads it can someone tell me how getResourceAsStream search the class path

谢谢

一个场景是: -
我的下面的代码是一个简单的程序,我的资源文件abc.txt在com.abc包中。当我指定它工作的包的路径时,当我没有它不起作用。

one scenario is :- My below code is a simple program and my resource file abc.txt is inside com.abc package. when i specify path of package it worked and when i did not it does not work.

package com.abc;

public class ResourceExp {

    public static void main(String args[])
    {
        new ResourceExp().getResource();
    }

    public void getResource()
    {
        String name = "abc.txt";
        // worked
        System.out.println(ResourceExp.class.getClassLoader().getResourceAsStream("com/abc/"+name));
        //not workded
        //System.out.println(ResourceExp.class.getClassLoader().getResourceAsStream(name));

    }

}    

如果getResourceAsStream看起来所有jar文件和目录中的资源然后我必须指定包路径

if getResourceAsStream looks the resource in all jar file and directory then why i have to specify the package path

推荐答案


I谷歌如何下面的代码加载资源
Abc.class.getClassLoader()。getResourceAsStream(abc.txt)并找到
,它在类$ b $中搜索所有jar文件和zip文件中的资源b path。

I google how below code loads the resource Abc.class.getClassLoader().getResourceAsStream("abc.txt") and find that it searchs the resource in all jar file and zip file in class path.

当你只使用一个ClassLoader(大多数非OSGi /非模块化环境)时,这是正确的。然后,所有JAR的所有内容都可以看作是一个大树,其中JAR的类和资源(在类路径之前出现)胜过JARS的那些,后者发生在后面。

Thats correct when you work only with a single ClassLoader (most non-OSGi/ non-modular environments). Then all content of all JARs can be seen as one big tree, where classes and resources of JARs, which occur prior in the class path, win over those of JARS, which occur further behind.


但是当我尝试它时我无法加载它但是如果我给包
路径然后我能够加载有人可以告诉我
getResourceAsStream如何搜索类路径

But when i tried it I am not able to loads it but if i give package path then I am able to loads it can someone tell me how getResourceAsStream search the class path



Abc.class.getClassLoader().getResourceAsStream("abc.txt")

在树根处搜索:

Abc.class.getResourceAsStream("abc.txt")

相对于Abc包的搜索。

searches relative to the package of Abc.

Abc.class.getResourceAsStream("/abc.txt")

再次搜索树的根目录。

所有这些方法只搜索指定的目录(或根目录),不会遍历和搜索整个树。

All these methode will only search in the specified directory (or the root directory) and won't traverse and search the whole tree.

就个人而言,我通常总是使用后两个版本( Class.getResourceAsStream )并且很少使用 ClassLoader.getResourceAsStr eam 方法直接。

Personally, I usually always use the latter two versions (Class.getResourceAsStream) and rarely use the ClassLoader.getResourceAsStream method directly.

这篇关于getClassLoader()。getResourceAsStream()如何在java中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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