Discuss / Java / 练习:递归打印所有文件和子文件夹的内容

练习:递归打印所有文件和子文件夹的内容

Topic source

jasmine

#1 Created at ... [Delete] [Delete and Lock User]
static void listDir(File dir, int i) throws IOException {    // TODO: 递归打印所有文件和子文件夹的内容    File[] fs = dir.listFiles();    String delimiter = "";    for (int j = 0; j<i; j++) {        delimiter += "  ";    }    if (fs != null) {        for (File f : fs) {            String file_tmp = delimiter + f.getName();            if (f.isDirectory()) {                System.out.println(file_tmp+"/");                listDir(f.getCanonicalFile(), i+1);            } else {                System.out.println(file_tmp);            }        }    }}

  • 1

Reply