getClassLoader().getResourceAsStream() 如何在 Java 中工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27155195/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How getClassLoader().getResourceAsStream() works in java
提问by yoga
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.
我谷歌下面的代码如何加载资源 Abc.class.getClassLoader().getResourceAsStream("abc.txt")
并发现它在类路径中的所有 jar 文件和 zip 文件中搜索资源。
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
但是当我尝试它时我无法加载它但是如果我提供包路径然后我可以加载它有人可以告诉我 getResourceAsStream 如何搜索类路径
Thanks
谢谢
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.
一种情况是:-我下面的代码是一个简单的程序,我的资源文件 abc.txt 在 com.abc 包中。当我指定包的路径时它工作,当我没有时它不起作用。
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));
}
}
if getResourceAsStream looks the resource in all jar file and directory then why i have to specify the package path
如果 getResourceAsStream 在所有 jar 文件和目录中查找资源,那么为什么我必须指定包路径
采纳答案by Puce
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.
我谷歌下面的代码如何加载资源 Abc.class.getClassLoader().getResourceAsStream("abc.txt") 并发现它在类路径中的所有 jar 文件和 zip 文件中搜索资源。
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.
当您仅使用单个 ClassLoader(大多数非 OSGi/非模块化环境)时,这是正确的。那么所有 JAR 的所有内容都可以看作是一棵大树,其中在类路径中出现在前面的 JAR 的类和资源战胜了出现在后面的 JARS 的类和资源。
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
但是当我尝试它时我无法加载它但是如果我提供包路径然后我可以加载它有人可以告诉我 getResourceAsStream 如何搜索类路径
Abc.class.getClassLoader().getResourceAsStream("abc.txt")
searches at root of the tree while:
在树的根部搜索,同时:
Abc.class.getResourceAsStream("abc.txt")
searches relative to the package of Abc.
搜索相对于 Abc 的包。
Abc.class.getResourceAsStream("/abc.txt")
searches at the root of the tree again.
再次搜索树的根部。
All these methode will only search in the specified directory (or the root directory) and won't traverse and search the whole tree.
所有这些方法只会在指定目录(或根目录)中搜索,不会遍历搜索整个树。
Personally, I usually always use the latter two versions (Class.getResourceAsStream
) and rarely use the ClassLoader.getResourceAsStream
method directly.
就我个人而言,我通常总是使用后两个版本(Class.getResourceAsStream
),很少ClassLoader.getResourceAsStream
直接使用该方法。
回答by Lorenzo Notaro
For example, you can make a "resources" source folder, put files in it, then use Thread.currentThread().getContextClassLoader().getResourceAsStream("abc.txt");
I always use this method.
比如你可以制作一个“resources”源文件夹,把文件放进去,然后用Thread.currentThread().getContextClassLoader().getResourceAsStream("abc.txt");
我一直用的这个方法。