Print the tree structure of a Java project file

package com.baidu.ueditor;

import java.io.File;

/**
 * 打印项目所有文件的树形结构
 */
class FileSystem {
    public static void main(String[] args) {
        // 指定文件位置
        File f = new File(System.getProperty("user.dir"));
        // 打印在这个文件下的文件夹;
        System.out.println(f.getName());
        // 方法!进入子文件夹中 并打印子文件名
        tree(f, 1);
    }

    private static void tree(File f, int level) {
        // 缩进量
        String preStr = "";
        for (int i = 0; i < level; i++) {
            if (i == level - 1) {
                preStr = preStr + "└─";
            } else {
                // 级别 - 代表这个目下下地子文件夹
                preStr = preStr + "|   ";
            }
        }
        // 返回一个抽象路径名数组,这些路径名表示此抽象路径名所表示目录中地文件
        File[] childs = f.listFiles();
        for (int i = 0; i < childs.length; i++) {
            // 打印子文件的名字
            System.out.println(preStr + childs[i].getName());
            // 测试此抽象路径名表示地文件能否是一个目录
            if (childs[i].isDirectory()) {
                // 假如子目录下还有子目录,递归子目录调用此方法
                tree(childs[i], level + 1);
            }
        }
    }
}

Partial screenshot of the result: 

Guess you like

Origin blog.csdn.net/Ciel_Y/article/details/123032922