【发布时间】:2021-09-02 07:42:30
【问题描述】:
我正在尝试将 json 文件作为 android 开发项目的一部分进行解析。这是不断弹出的错误消息:
W/System.err: java.nio.file.NoSuchFileException: C:/Users/andno/Desktop/AndroidDev2ClimateApp/app/src/main/java/com/example/androiddev2climateapp/ui/home/test.json
但是,在 android studios 中,错误中的路径作为链接提供,我可以单击该链接,将我引导至写入文件 - 因此,我认为路径没有错。
这是我的代码:
String first = "C:/Users/andno/Desktop/AndroidDev2ClimateApp/app/src/main/java/com/example/androiddev2climateapp/ui/home/test.json";
try {
String fileContents = new String((Files.readAllBytes(Paths.get(first))));
JSONObject json = new JSONObject(fileContents);
JSONArray widgets = new JSONArray("WidgetArray");
for(int i = 0; i < widgets.length(); i++){
int id = widgets.getJSONObject(i).getInt("id");
System.out.println(id);
}
} catch(IOException | JSONException e){
e.printStackTrace();
}
很抱歉,格式混乱了。我也尝试在路径中使用 \ 而不是 / ,但它不起作用。任何帮助将不胜感激 - 非常感谢!
【问题讨论】:
-
尝试打印出文件对象的绝对路径。
-
试着把斜线换成另一种方式?您需要将它们加倍才能在字符串中转义它们,所以
"C:\\Users\\" -
为什么要在 Android 项目中从 Windows 文件系统打开文件?
-
看看this是不是你想要做的。
标签: java android nosuchfileexception