Eclipse 找不到文件

Eclipse can't find files

我正试图在 java 上编写一个简单的程序,突然我无法打开任何文件。我编写了一个程序来查看 eclipse 是否可以找到该文件 - 但它也没有用。但是当我在 CMD 中尝试它时,它工作得很好。为什么?谢谢。 示例:http://prntscr.com/7qn9gm

要访问特定包中的文件,您可以这样做

来自静态方法

File file = new File(Yourclass.class.getResource("/your/package/name/filename"));

所以在你的情况下

File file = new File(Main.class.getResource("/textures/icon.png"));

从非静态方法,你可以做

File file = new File(getClass().getResource("/textures.icon.png"));

eclipse 项目的基本路径是它的根文件夹。尝试将文件路径更改为 "src/textures/icon.png".

像这样初始化文件实例

File file = new File(System.getProperty("user.dir")+"/src/textures/icon.png");

System.getProperty("user.dir") returns 项目文件夹的路径。