• 生成文件树形结构


    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/>
    ");
                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 + "
    ");
                        listDirTree(list, tmpFile, level + 1, sb);
                    } else {
                        sb.append(getBlankString(level) + END_STRING + tmpFile.name + "
    ");
                    }
    
                }
            }
        }
    
        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

  • 相关阅读:
    springmvc入门详解
    getClass 与getSimpleName
    mybati的存储过程
    mybatis与spring的整合
    mybatis分页插件以及懒加载
    mybatis知识总结
    【Java面试题】30 子线程循环10次,接着主线程循环100,接着又回到子线程循环10次,接着再回到主线程又循环100,如此循环50次,请写出程序。
    【Java面试题】29 设计4个线程,其中两个线程每次对j增加1,另外两个线程对j每次减少1。写出程序。
    【Java面试题】28 简述synchronized和java.util.concurrent.locks.Lock的异同 ?
    【Java面试题】27 多线程笔试面试概念问答
  • 原文地址:https://www.cnblogs.com/zincredible/p/14196873.html
Copyright © 2020-2023  润新知