eclipse Eclipse如何引用同一项目下不同src中的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7364882/
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-09-19 16:58:30  来源:igfitidea点击:

Eclipse how to reference file in a different src under the same project

eclipsefilereferencesrcfilereference

提问by jason

My current setup in eclipse is like this:

我目前在 eclipse 中的设置是这样的:

trunk
--working/src
--resources

主干
--working/src
--resources

I have a java file inside a package under working/src and I am trying to retrieve a file in the resources. The path I am using is "../resources/file.txt". However I am getting an error saying that the file does not exist.

我在working/src 下的包中有一个java 文件,我正在尝试检索资源中的文件。我使用的路径是“../resources/file.txt”。但是我收到一个错误,说该文件不存在。

Any help would be appreciated thanks!

任何帮助将不胜感激谢谢!

回答by Surendra Jnawali

Considering your structure

考虑你的结构

I have package like

我有这样的包裹

trunk

树干

  • working/src/FileRead.java
  • resources/name list.txt
  • 工作/src/FileRead.java
  • 资源/名称列表.txt

Following Code might solve your problem

以下代码可能会解决您的问题

 package working.src;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;

public class FileRead {
    public FileRead() {
        URL url = getClass().getResource("/resources/name list.txt");
        try {
            BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
            String nameList;
            while ((nameList = in.readLine()) != null) {
                System.out.println(nameList);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new FileRead();
    }
}

回答by Garrett Hall

Files will be referenced relative to your project path, so use "resources/file.txt" to reference the file.

文件将相对于您的项目路径进行引用,因此请使用“resources/file.txt”来引用该文件。

However, if you want the file to be accessible when you export the program as a JAR, the path "resources/file.txt" must exist relative to your JAR.

但是,如果您希望在将程序导出为 JAR 时可以访问该文件,则路径“resources/file.txt”必须相对于您的 JAR 存在。

回答by mliebelt

It depends on how you have specified your Java Build Path inside eclipse. I have tested two setups with different results:

这取决于您如何在 eclipse 中指定 Java Build Path。我测试了两种具有不同结果的设置:

  1. Define the directory working/srconly as build path. You can get the information what is in your build path through: Select project > Properties > Java Build Path > Tab Source. There are all source folders listed. You see there that there is a default output folder defined. All resources that are contained in a build path are copied to the default output folder, and are then available for the eclipse classes there. You can check that in the resource perspective in your bin folder if the resource is copied there. In this setup, only the classes are generated, resources are not copied (and therefore not found).
  2. Define the directory working/srcand resourcesas build path. Then the resource is copied and may then found by the path "file.txt"
  1. working/src仅将目录定义为构建路径。你可以得到什么是你构建路径通过信息:Select project > Properties > Java Build Path > Tab Source。列出了所有源文件夹。您会看到那里定义了一个默认的输出文件夹。构建路径中包含的所有资源都被复制到默认输出文件夹,然后可用于那里的 eclipse 类。如果资源被复制到那里,您可以在 bin 文件夹的资源透视图中检查。在此设置中,仅生成类,不复制资源(因此找不到)。
  2. 定义目录working/srcresources作为构建路径。然后复制资源,然后可以通过路径“file.txt”找到

So eclipse has a simple build process included and hides that from the developer, but it is a build process nonetheless.

因此,eclipse 包含一个简单的构建过程,并且对开发人员隐藏了该过程,但它仍然是一个构建过程。