gpt4 book ai didi

java - 无法访问 eclipse 中的文件给出 : FileNotFoundException

转载 作者:行者123 更新时间:2023-11-30 10:15:24 26 4
gpt4 key购买 nike

在 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 文件夹。我怎样才能得到我的文件,请告诉我。

最佳答案

您可以使用以下两种之一:

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

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

然后尝试使用以下方式访问该文件:

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

希望对您有所帮助。

关于java - 无法访问 eclipse 中的文件给出 : FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50469685/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com