生成文件树形结构

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class FileTreeUtil {

    public static final String END_STRING = "|__";

    /**
     * 打印文件夹树状结构
     * 
     * @param dirPath
     * @param level
     * @throws IOException
     */
    public static void listDirTree(String dirPath, int level) throws IOException {
        File file = new File(dirPath);
        if (file.isFile()) {
            System.out.println(getBlankString(level) + END_STRING + file.getName());
            return;
        }
        File[] files = file.listFiles();
        for (File tmpFile : files) {
            if (tmpFile.isDirectory()) {
                System.out.println(getBlankString(level) + END_STRING + tmpFile.getName());
                listDirTree(tmpFile.getCanonicalPath(), level + 1);
            } else {
                System.out.println(getBlankString(level) + END_STRING + tmpFile.getName());
            }
        }
    }

    public static String getBlankString(int level) {
        String str = "--";
        for (int i = 0; i < level; i++) {
            str += "---";
        }
        return str;
    }

    static class FileInfo {
        public String id;
        //父id
        public String pid;
        public String name;
        public boolean isDir;

        public FileInfo(String id, String pid, String name, boolean isDir) {
            super();
            this.id = id;
            this.pid = pid;
            this.name = name;
            this.isDir = isDir;
        }

        public FileInfo() {
            super();
        }
    }

    /**
     * 打印list树状结构,FileInfo关联父子关系
     * @param list
     * @param root
     *            id为0的是root
     * @param level
     */
    public static void listDirTree(List<FileInfo> list, FileInfo root, int level, StringBuilder sb) {
        if (!root.isDir) {
            sb.append(getBlankString(level) + END_STRING + root.name + "<br/>\n");
            return;
        }
        for (FileInfo tmpFile : list) {
            if (tmpFile.pid.equals(root.id)) {
                if (tmpFile.isDir && !"0".equals(tmpFile.id)) {
                    sb.append(getBlankString(level) + END_STRING + tmpFile.name + "\n");
                    listDirTree(list, tmpFile, level + 1, sb);
                } else {
                    sb.append(getBlankString(level) + END_STRING + tmpFile.name + "\n");
                }

            }
        }
    }

    public static String getUUID() {
        return UUID.randomUUID().toString().replaceAll("-", "").substring(0, 8);
    }

    public static void main(String[] args) throws IOException {
        //listDirTree("E:\\code\\niiam-project\\cloud-3d-app\\app\\src\\main\\java", 0);
        List<FileInfo> list = new ArrayList<>();
        FileInfo root = new FileInfo("0", "-1", "Docs", true);
        list.add(root);
        list.add(new FileInfo("1", "0", "1", true));
        list.add(new FileInfo("2", "0", "2", true));
        list.add(new FileInfo("3", "0", "3", true));

        list.add(new FileInfo("11", "1", "11", true));
        list.add(new FileInfo("22", "2", "22", true));
        list.add(new FileInfo("33", "3", "33", true));
        list.add(new FileInfo("44", "1", "44", true));
        list.add(new FileInfo("55", "1", "55", true));
        list.add(new FileInfo("66", "1", "66", true));

        list.add(new FileInfo("111", "11", getUUID() + ".java", false));
        list.add(new FileInfo("222", "11", getUUID() + ".java", false));
        list.add(new FileInfo("333", "11", getUUID() + ".java", false));
        list.add(new FileInfo("444", "11", getUUID() + ".java", false));

        list.add(new FileInfo(getUUID(), "0", getUUID() + ".c", false));
        list.add(new FileInfo(getUUID(), "0", getUUID() + ".c", false));
        list.add(new FileInfo(getUUID(), "0", getUUID() + ".c", false));

        list.add(new FileInfo(getUUID(), "1", getUUID() + ".py", false));
        list.add(new FileInfo(getUUID(), "2", getUUID() + ".py", false));
        list.add(new FileInfo(getUUID(), "3", getUUID() + ".py", false));
        
        System.out.println("|__Docs");
        StringBuilder sb = new StringBuilder();
        listDirTree(list, root, 1, sb);
        System.out.println(sb.toString());
    }
}

效果:

|__Docs
-----|__1
--------|__11
-----------|__68391d65.java
-----------|__68334e67.java
-----------|__5cc92bb6.java
-----------|__756283d5.java
--------|__44
--------|__55
--------|__66
--------|__6f4f9617.py
-----|__2
--------|__22
--------|__536037ce.py
-----|__3
--------|__33
--------|__1ae6670d.py
-----|__0c3f7888.c
-----|__5f940aec.c
-----|__d329743b.c

 

上一篇:Go递归修改文件名


下一篇:Flask08 包含(include)、继承(extends)、宏???、模板中变量的来源、利用bootstrap构建自己的网页结构