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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 16:38:53  来源:igfitidea点击:

Project root folder path

javafile-iopath

提问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 IOExceptionwith unknown source

我得到一个IOExceptionunknown source

采纳答案by Killerpixler

Turns out Eclipse puts the entire content of the resfolder in the root folder of the binfolder. Therefore the path is just very simply /file.xml. I tried putting it into a subfolder of res called xmlsand 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文件夹。