Eclipse无法找到文件

时间:2015-07-09 11:42:54

标签: java file io

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

3 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

要访问特定包中的文件,您可以执行

来自静态方法

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"));

答案 2 :(得分:0)

像这样初始化文件实例

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

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