java - 无法在 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 文件夹。我怎样才能得到我的文件,请告诉我。
解决方案
您可以使用以下两种中的任何一种:
String location=System.getProperty("user.dir");
或者
String location= new java.io.File(".").getCanonicalPath();
然后尝试使用以下命令访问该文件:
FileReader file = new FileReader(location+"\\configuration.json");
希望能帮助到你。
推荐阅读
- android - 如何修复我的 RecyclerView 的 SearchView 过滤结果上的项目点击?
- google-apps-script - 使用应用程序脚本更新一系列单元格中的公式
- python - ValueError:查看限制最小值-36834.700000?
- go - 什么是名为 runtime.gopark 的 goroutine?
- python - Excel Pandas DataFrame 到嵌套 JSON
- sympy - Sympy:敏感性分析
- python - 多处理:使用 tqdm 显示全局进度条?
- snowflake-cloud-data-platform - 雪花(私人链接)是否支持自定义域?
- java - 在 MVC 程序的视图中将类型转换为列表的最佳方法是什么?
- laravel - 无法解析您的关键护照 laravel