Java Eclipse 项目的默认根文件夹

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

Default root folder for an Eclipse project

javaeclipsefile

提问by Jonathan

I am attempting to read a file as follows:

我正在尝试按如下方式读取文件:

File imgLoc = new File("Player.gif");

BufferedImage image = null;

try {
    image = ImageIO.read(imgLoc);
}
catch(Exception ex)
{
    System.out.println("Image read error");
    System.exit(1);
}
return image;

I have a file named "Player.gif" as shown in imgLoc, but I do not know where to put the file. Where will Eclipse look to find the file when I attempt to read it?

我有一个名为“Player.gif”的文件,如 imgLoc 所示,但我不知道该文件放在哪里。当我尝试读取文件时,Eclipse 将在哪里查找文件?

Also, is there a better way of creating a BufferedImage from an image file stored somewhere amongst the project directories? (I.e., a better way of reading an image than through File, or a better method within File to create a BufferedImage).

另外,是否有更好的方法从存储在项目目录中某处的图像文件创建 BufferedImage ?(即,比通过 File 读取图像更好的方法,或者在 File 中创建 BufferedImage 的更好方法)。

采纳答案by tangens

In the run dialog you can choose the directory. The default is the project root.

在运行对话框中,您可以选择目录。默认是项目根。

回答by Mikael Ohlson

Are you trying to write a plugin for Eclipse or is it a regular project?

您是要为 Eclipse 编写插件还是常规项目?

In the latter case, wouldn't that depend on where the program is installed and executed in the end?

在后一种情况下,这不取决于程序最终安装和执行的位置吗?

While trying it out and running it from Eclipse, I'd guess that it would find the file in the project workspace. You should be able to find that out by opening the properties dialog for the project, and looking under the Resourceentry.

在尝试并从 Eclipse 运行它时,我猜它会在项目工作区中找到该文件。您应该能够通过打开项目的属性对话框并查看资源条目下找到它。

Also, you can add resources to a project by using the Importmenu option.

此外,您可以使用“导入”菜单选项向项目添加资源。

回答by pstanton

From my experience it seems to be the containing projects directory by default, but there is a simple way to find out:

根据我的经验,默认情况下它似乎是包含项目的目录,但有一种简单的方法可以找出:

System.out.println(new File(".").getAbsolutePath());

回答by Wayne Beaton

Take a look in the comments for Class.getResource and Class.getResourceAsStream. These are probably what you really want to use as they will work whether you are running from within the directory of an Eclipse project, or from a JAR file after you package everything up.

查看 Class.getResource 和 Class.getResourceAsStream 的注释。这些可能是您真正想要使用的,因为无论您是从 Eclipse 项目的目录中运行,还是在打包所有内容后从 JAR 文件中运行,它们都将起作用。

You use them along the lines of:

您可以按照以下方式使用它们:

InputStream in = MyClass.class.getResourceAsStream("Player.gif");

In this case, Java would look for the file "Player.gif" next to the MyClass.class file. That is, if the full package/class name is "com.package.MyClass", then Java will look for a file in "[project]/bin/com/package/Player.gif". The comments for getResourceAsStream indicate that if you lead with a slash, i.e. "/Player.gif", then it'll look in the root (i.e. the "bin" directory).

在这种情况下,Java 将查找 MyClass.class 文件旁边的文件“Player.gif”。也就是说,如果完整的包/类名称是“com.package.MyClass”,那么 Java 将在“[project]/bin/com/package/Player.gif”中查找文件。getResourceAsStream 的注释表明,如果您以斜杠开头,即“/Player.gif”,那么它将在根目录(即“bin”目录)中查找。

Note that you can drop the file in the "src" directory and Eclipse will automatically copy it to the "bin" directory at build time.

请注意,您可以将文件放在“src”目录中,Eclipse 会在构建时自动将其复制到“bin”目录中。