简体   繁体   English

Eclipse中文件的绝对/相对路径

[英]Absolute/Relative path to files in Eclipse

I am working on an assignment where I need to use an XML file in the project. 我正在做一个分配,我需要在项目中使用XML文件。 The file is currently in a folder I created called res . 该文件当前位于我创建的名为res的文件夹中。 The res folder is simply under the project folder. res文件夹仅在项目文件夹下。 myproject --> res --> myxmlfile.xml

The .java file calling this xml file is in the src folder. 调用此xml文件的.java文件位于src文件夹中。 myproject --> JavaResources --> src --> edu.unsw.comp9321.assignment1(package) --> myjavafile.java

I was wondering how should I refer to this file in my Java code if I also want this code to work when I run the project on another computer? 我想知道如果我还希望在另一台计算机上运行该项目时该代码也能工作,该如何在Java代码中引用该文件?

When I use a relative path it appears to be searching from the folder where Eclipse is installed, as opposed to the workspace folder where the project exists. 当我使用相对路径时,它似乎是从安装Eclipse的文件夹中搜索的,而不是项目所在的workspace文件夹。

This is what I have at the moment: 这是我目前所拥有的:

File fXmlFile = new File("C:/Users/Giridhaar/workspace/COMP9321Assignment1/res/xml/musicDb.xml");

Thank you for your help. 谢谢您的帮助。

这应该为您工作:

File fXmlFile = new File("res/xml/musicDb.xml");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM