遍历过程不知道哪出错了,也没捕获到异常。 public static void addFileToJson(String filePath, JSONObject json) {
try {
File file = new File(filePath);
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
JSONObject jsonObj = new JSONObject();
json.put(files[i].getName(), jsonObj);
jsonObj.put("Time", (files[i].lastModified()));
if (files[i].isDirectory()) {
jsonObj.put("isDir", true);
addFileToJson(files[i].getPath(), jsonObj);
} else {
jsonObj.put("isDir", false);
}
}
} catch(Exception e) {
e.printStackTrace();
}
}
下面是调用
JSONObject json = new JSONObject(); 
JSONObject root = new JSONObject();
json.put("存储卡", root);
root.put("isDir", true);
addFileToJson("/sdcard/", root);