我有用于读取JSON文件的代码:
Gson gson = new Gson();
BufferedReader bufferedReader = null;
try {
bufferedReader = new BufferedReader(new FileReader(path));
JsonObject data = gson.fromJson(bufferedReader, JsonObject.class);
Log.d(TAG, "DATA: " + data.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
该代码似乎可以正常工作,但是在logcat中,我收到了以下3行:
com.example.package W/System: A resource failed to call close.
com.example.package W/System: A resource failed to call end.
com.example.package W/System: A resource failed to call close.
是正常现象还是我做错了什么?预先非常感谢。
答案 0 :(得分:-1)
好吧,@ deHaar是正确的:
try (BufferedReader bufferedReader = new BufferedReader(new FileReader(path))) {
JsonObject data = gson.fromJson(bufferedReader, JsonObject.class);
Log.d(TAG, "DATA: " + data.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
这样,我不会收到警告。但是我不明白为什么。如果有人可以在评论中写出来,我将不胜感激。谢谢。