Discuss / Java / 一种实现

一种实现

Topic source
public class Main {
    public static void main(String[] args) throws IOException {
        File file = new File("D:\\workspaces\\HelloWorld\\out");
        String str = "";
        System.out.println(file.getName());
        printChildFile(file, str);
    }
    static void printChildFile(File file, String str) {
        if (file.isDirectory()) {
            str = str + "  ";
            File[] files = file.listFiles();
            if (files != null) {
                for (File f : files) {
                    if (f.isFile()) {
                        System.out.println(str + f.getName());
                    } else if (f.isDirectory()) {
                        System.out.println(str + f.getName());
                        printChildFile(f, str);
                    }
                }
            }
        }
    }
}

文件夹输出修改:

System.out.println(str + f.getName() + "/");

江舟独行

#3 Created at ... [Delete] [Delete and Lock User]
 else if (f.isDirectory()) {    System.out.println(str + f.getName() + "/");    printChildFile(f, str);//需要用到递归调用,否则子目录中的文件无法遍历到

  • 1

Reply