Java 项目根文件夹路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19383004/
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
Project root folder path
提问by Killerpixler
I have a class in a package com.mwerner.utils that needs the path for an getResourceAsStream()
call
我在包 com.mwerner.utils 中有一个类需要getResourceAsStream()
调用路径
the file it is supposed to load is located in a subfolder of the project root i.e.:
它应该加载的文件位于项目根目录的子文件夹中,即:
/src/com/mwerner/utils/myfile.java has to load
/src/com/mwerner/utils/myfile.java 必须加载
/res/file.xml
/res/file.xml
I tried stuff like
我试过类似的东西
/res/file.xml
../res/file.xml
res/file.xml
What is the right one?
什么是正确的?
EDIT:
编辑:
I am using Xstream to parse the XML into objects. The line of code in question is:
我正在使用 Xstream 将 XML 解析为对象。有问题的代码行是:
ObjectInputStream in = xstream.createObjectInputStream(Utils.class.getResourceAsStream("res/file.xml"));
I get an IOException
with unknown source
我得到一个IOException
与unknown source
采纳答案by Killerpixler
Turns out Eclipse puts the entire content of the res
folder in the root folder of the bin
folder. Therefore the path is just very simply /file.xml
. I tried putting it into a subfolder of res called xmls
and the path then is /xmls/file.xml
结果 Eclipse 将res
文件夹的全部内容放在文件夹的根文件bin
夹中。所以路径只是很简单/file.xml
。我试着把它放到 res 的子文件夹中,xmls
然后路径是/xmls/file.xml
I also went to the source tab of the java build path and added the res folder.
我还转到了java构建路径的源选项卡并添加了res文件夹。