简体   繁体   English

定位 Eclipse 项目的默认根文件夹

[英]Locating default root folder for an Eclipse project

I am reading 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 do not know where to place my file to make the Eclipse IDE, and my project can detect it when I run my code.我不知道把我的文件放在哪里来制作 Eclipse IDE,我的项目在运行我的代码时可以检测到它。

Is there a better way of creating a BufferedImage from an image file stored in your project directory?是否有更好的方法从存储在项目目录中的图像文件创建 BufferedImage?

Take a look in the comments for Class.getResource and Class.getResourceAsStream.查看 Class.getResource 和 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.这些可能是您真正想要使用的,因为无论您是从 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.在这种情况下,Java 将查找 MyClass.class 文件旁边的文件“Player.gif”。 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".也就是说,如果完整的包/类名称是“com.package.MyClass”,那么 Java 将在“[project]/bin/com/package/Player.gif”中查找文件。 The comments for getResourceAsStream indicate that if you lead with a slash, ie "/Player.gif", then it'll look in the root (ie the "bin" directory). 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”目录中。

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

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

In the run dialog you can choose the directory.在运行对话框中,您可以选择目录。 The default is the project root.默认是项目根。

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.在尝试并从 Eclipse 运行它时,我猜它会在项目工作区中找到该文件。 You should be able to find that out by opening the properties dialog for the project, and looking under the Resource entry.您应该能够通过打开项目的属性对话框并查看资源条目下找到它。

Also, you can add resources to a project by using the Import menu option.此外,您可以使用“导入”菜单选项向项目添加资源。

The default root folder for any Eclipse project is also a relative path of that application.任何 Eclipse 项目的默认根文件夹也是该应用程序的相对路径

Below are steps I used for my Eclipse 4.8.0 and Java 1.8 project.下面是我用于Eclipse 4.8.0Java 1.8项目的步骤。

I - Place your file you want to interact with along the BIN and SRS folders of your project and not in one of those folders. I - 将要与之交互的文件放在项目的 BIN 和 SRS 文件夹中,而不是放在这些文件夹之一中。

II - Implement below code in your main() method. II - 在 main() 方法中实现以下代码。

public static void main(String [] args) throws IOException  {
    
FileReader myFileReader;
BufferedReader myReaderHelper;
    
try {
    
String localDir = System.getProperty("user.dir");
myFileReader = new FileReader(localDir + "\\yourFile.fileExtension");
myReaderHelper = new BufferedReader(myFileReader);

if (myReaderHelper.readLine() != null)  {
    
StringTokenizer myTokens =
    new StringTokenizer((String)myReaderHelper.readLine(), "," );
    System.out.println(myTokens.nextToken().toString()); // - reading first item
}
} catch (FileNotFoundException myFileException) {
    myFileException.printStackTrace();  }     } // End of main()

III - Implement a loop to iterate through elements of your file if your logic requires this. III - 如果您的逻辑需要,则实现循环以遍历文件的元素。

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

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