首页 > 解决方案 > 无法在 Eclipse 中访问文件给出:FileNotFoundException

问题描述

在 Eclipse 中,我试图给出一个 configuration.json 文件的路径,但它没有接受它。

System.getProperty("user.dir") 

C:\Users\cmalik\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Eclipse

try {
            File f = new File("."); // current directory

            File[] files = f.listFiles();
            for (File file : files) {
                if (file.isDirectory()) {
                    System.out.print("directory:");
                } else {
                    System.out.print("     file:");
                }
                System.out.println(file.getCanonicalPath());
        }

file:C:\Users\cm\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Eclipse\Eclipse Jee Neon.lnk
file:C:\Users\cm\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Eclipse\hs_err_pid10180.log
file:C:\Users\cm\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Eclipse\hs_err_pid9728.log

我的项目结构是

ProjectName
  ---src
      ---- com.abc.def.ghi.jkl.WebServices
                              ------ myService.java
  ---configuration.json

我尝试访问文件的代码是:

FileReader file = new FileReader("configuration.json");
or
FileReader file = new FileReader("projectName\\configuration.json");  

它无法作为 System.getProperty("user.dir") 的输出访问是 eclipse 文件夹而不是我当前的 projectName 文件夹。我怎样才能得到我的文件,请告诉我。

标签: javaeclipse

解决方案


您可以使用以下两种中的任何一种:

String location=System.getProperty("user.dir");

或者

String location= new java.io.File(".").getCanonicalPath();

然后尝试使用以下命令访问该文件:

FileReader file = new FileReader(location+"\\configuration.json");

希望能帮助到你。


推荐阅读