Java 从类路径资源(XML 文件)获取输入流
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/793213/
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
Getting the inputstream from a classpath resource (XML file)
提问by Veera
In Java web application, Suppose if I want to get the InputStream of an XML file, which is placed in the CLASSPATH (i.e. inside the sourcesfolder), how do I do it?
在Java Web 应用程序中,假设我想获取一个XML 文件的InputStream,该文件位于CLASSPATH(即sources文件夹内)中,我该怎么做?
采纳答案by cletus
ClassLoader.getResourceAsStream()
.
ClassLoader.getResourceAsStream()
.
As stated in the comment below, if you are in a multi-ClassLoader
environment (such as unit testing, webapps, etc.) you may need to use Thread.currentThread().getContextClassLoader()
. See http://stackoverflow.com/questions/2308188/getresourceasstream-vs-fileinputstream/2308388#comment21307593_2308388.
如下面的评论所述,如果您处于多ClassLoader
环境(例如单元测试、webapps 等)中,您可能需要使用Thread.currentThread().getContextClassLoader()
. 见http://stackoverflow.com/questions/2308188/getresourceasstream-vs-fileinputstream/2308388#comment21307593_2308388。
回答by mP.
someClassWithinYourSourceDir.getClass().getResourceAsStream();
someClassWithinYourSourceDir.getClass().getResourceAsStream();
回答by Aaron Digulla
That depends on where exactly the XML file is. Is it in the sources folder (in the "default package" or the "root") or in the same folder as the class?
这取决于 XML 文件的确切位置。它是在源文件夹中(在“默认包”或“根”中)还是在与类相同的文件夹中?
In for former case, you must use "/file.xml
" (note the leading slash) to find the file and it doesn't matter which class you use to try to locate it.
在前一种情况下,您必须使用“ /file.xml
”(注意前导斜杠)来查找文件,并且您使用哪个类来尝试定位它并不重要。
If the XML file is next to some class, SomeClass.class.getResourceAsStream()
with just the filename is the way to go.
如果 XML 文件在某个类旁边,那么SomeClass.class.getResourceAsStream()
只需要文件名即可。
回答by Clint
ClassLoader.class.getResourceAsStream("/path/to/your/xml")
and make sure that your compile script is copying the xml file to where in your CLASSPATH.
ClassLoader.class.getResourceAsStream("/path/to/your/xml")
并确保您的编译脚本将 xml 文件复制到 CLASSPATH 中的位置。
回答by jkarretero
ClassLoader.class.getResourceAsStream("/path/file.ext");
回答by user64141
Some of the "getResourceAsStream()" options in this answer didn't work for me, but this one did:
这个答案中的一些“getResourceAsStream()”选项对我不起作用,但这个选项对我有用:
SomeClassWithinYourSourceDir.class.getClassLoader().getResourceAsStream("yourResource");
SomeClassWithinYourSourceDir.class.getClassLoader().getResourceAsStream("yourResource");
回答by Striker
I tried proposed solution and forward slash in the file name did not work for me, example: ...().getResourceAsStream("/my.properties"); null was returned
我尝试了建议的解决方案,文件名中的正斜杠对我不起作用,例如: ...().getResourceAsStream("/my.properties"); 返回空值
Removing the slash worked: ....getResourceAsStream("my.properties");
删除斜线工作: ....getResourceAsStream("my.properties");
Here is from doc API: Before delegation, an absolute resource name is constructed from the given resource name using this algorithm:
这是来自 doc API:在委托之前,使用此算法从给定的资源名称构造绝对资源名称:
If the name begins with a '/' ('\u002f'), then the absolute name of the resource is the portion of the name following the '/'.
Otherwise, the absolute name is of the following form:
modified_package_name/name
Where the modified_package_name is the package name of this object with '/' substituted for '.' ('\u002e').