我应该如何在 Java 中使用 getResource()?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6400646/
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 should I use getResource() in Java?
提问by Chap
This question is asked in numerous places, with myriad small variations. (Such as Java - getClassLoader().getResource() driving me bonkersamong others.) I still can't make it work.
Here's a code snippet:
这个问题在很多地方被问到,有无数的小变化。(例如Java - getClassLoader().getResource() 使我发疯。)我仍然无法让它工作。
这是一个代码片段:
String clipName = "Chook.wav";
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
// URL url = classLoader.getResource(clipName);
URL url = new URL("file:///Users/chap/Documents/workspace/the1620/bin/ibm1620/" + clipName);
ais = AudioSystem.getAudioInputStream(url);
This works -- note that I've hard-coded the path to the directory containing the clip file, which isthere, and isin the same directory as my .class file. Alas, the commented-out code just returns a null value for url.
此作品-请注意,我硬编码的路径,包含剪辑文件,该文件的目录是有,而且是在同一目录作为我的.class文件。唉,注释掉的代码只是为 url 返回一个空值。
Most other posts seem to deal with getResourceAsStream(). I think I'm supposed to be using getResource(). Is thatmaking the difference?
大多数其他帖子似乎都在处理 getResourceAsStream()。我想我应该使用 getResource()。这有区别吗?
It just can'tbe this hard. Any clues?
它只是不能这么难。有什么线索吗?
采纳答案by Mark Peters
String clipName = "Chook.wav";
When using getResource
, the string you pass in must be either an absolute name or be valid relative to a certain class. Since you're using ClassLoader.getResource()
and not Class.getResource()
, it must be an absolute path.
使用时getResource
,您传入的字符串必须是绝对名称或相对于某个类有效。由于您使用的是ClassLoader.getResource()
and not Class.getResource()
,因此它必须是绝对路径。
Without seeing your actual file hierarchy, I can only guess that "bin" is the root of your compiled classes and resources, and "ibm1260" is a package/folder within that path, and "Chook.wav" exists in that folder. If that's the case, then you need to use /ibm1260/Chook.wav
(or potentially ibm1260/Chook.wav
, I don't typically use the class loader for resource lookups) as the name of the file that you pass in to getResource()
.
没有看到您的实际文件层次结构,我只能猜测“bin”是您编译的类和资源的根,“ibm1260”是该路径中的包/文件夹,而“Chook.wav”存在于该文件夹中。如果是这种情况,那么您需要使用/ibm1260/Chook.wav
(或者可能ibm1260/Chook.wav
,我通常不使用类加载器进行资源查找)作为您传递给getResource()
.
Either way, you need to make sure that the file is copied into the location of your compiled code and that the root folder is on the classpath.
无论哪种方式,您都需要确保将文件复制到已编译代码的位置,并且根文件夹位于类路径中。
回答by Stephen C
The getResource
and getResourceAsStream
methods are for accessing resources on the classpath. You seem to be trying to access some resource that is not on the classpath.
该getResource
和getResourceAsStream
方法是访问资源的类路径。您似乎正在尝试访问不在类路径上的某些资源。
The logic that getResource
and getResourceAsStream
use to locate resources is essentially the same. The difference between the methods is that one returns a URL, and the other an InputStream
.
getResource
和getResourceAsStream
用来定位资源的逻辑本质上是一样的。这两种方法的区别在于一种返回 URL,另一种返回InputStream
.
It just can't be this hard. Any clues?
不可能这么难。有什么线索吗?
This is not that hard at all. You just need to understand how classpaths work ... and make sure that you use a resource name that resolves to a resource that you've put in the correct location in one of the directories or JAR files on the classpath.
这一点也不难。您只需要了解类路径的工作原理……并确保您使用的资源名称解析为您已放置在类路径上的目录或 JAR 文件之一中的正确位置的资源。
Or if the resource is not "part of" your application, don't access it this way.
或者,如果资源不是您的应用程序的“一部分”,请不要以这种方式访问它。