• java遍历文件夹并生成_sidebar.md


    
     
    import com.fasterxml.jackson.databind.ObjectMapper;
    
    import java.io.*;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Date;
    import java.util.Iterator;
    
    public class FileSystem {
        //public static final String path = "D:\\OneDrive\\java\\java笔记";
        public static final String path = "D:\\OneDrive\\java\\notebook - 副本";
        public static final String[] excludeDir = new String[]{".git","notebook_asset"};
    
        public static void main(String[] args) throws IOException {
            File f = new File(path);// 指定文件位置
            ArrayList<MDClass> mdClasses = new ArrayList<>();        // 获取所有文件
            ArrayList<String> mdSidebar = new ArrayList<>();          // 获取侧边栏字符串
            tree(f, 1, mdClasses,mdSidebar);
            // 转成无限极分类的样式
            ArrayList<MDClass> list = tree2(mdClasses, "");
    
            /**
             * 生成目录json
             */
            genderJson(list);
    
            /**
             * 生成_sidebar.md
             */
            genderSidebar(mdSidebar);
        }
    
        private static void genderSidebar(ArrayList<String> mdSidebar) throws IOException {
            String str = "";
            for (String s : mdSidebar) {
                str += s +"\r\n";
            }
            // 复制目录文件
            copyContent( path + "\\_sidebar.md", path + "\\_sidebar_old_" + String.valueOf(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())) + ".md");
    
            // 输出到目录文件
            File file = new File(path + "\\_sidebar.md");
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(str.getBytes());
            fos.flush();
            fos.close();
        }
    
        private static void genderJson(ArrayList<MDClass> list) throws IOException {
            // 使用JSON对象 将JSON字符串反序列化为JavaBean
            ObjectMapper om=new ObjectMapper();
            String json = om.writeValueAsString(list);
            System.out.println(json);
    
    
    
            // 复制目录文件
            copyContent( path + "\\content.json", path + "\\content_old_" + String.valueOf(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())) + ".json");
    
            // 输出到目录文件
            File file = new File(path + "\\content.json");
            FileOutputStream fos = new FileOutputStream(file);
            fos.write(json.getBytes());
            fos.flush();
            fos.close();
        }
    
    
    
        private static void copyContent( String fromPath, String goPath) throws IOException {
            File fromFile = new File(fromPath);
            //如果文件不存在则创建文件
            if (!fromFile.exists()) {
                fromFile.createNewFile();
            }
            File goFile = new File(goPath);
            //如果文件不存在则创建文件
            if (!goFile.exists()) {
                goFile.createNewFile();
            }
    
            //创建输入流,读取fromFile
            InputStream in = new FileInputStream(fromFile);
    
            //创建输入流,写入到goFile
            OutputStream out = new FileOutputStream(goFile);
    
            byte[] buf = new byte[2048];
            int i = 0;
            while ((i = in.read(buf)) != -1) {
                out.write(buf, 0, i);
                out.flush();
            }
            out.close();
            in.close();
            System.out.println("文件复制结束!!!");
        }
    
    
        private static ArrayList<MDClass> tree2(ArrayList<MDClass> list, String parentPath) {
            ArrayList<MDClass> newlist = new ArrayList<>();
            Iterator<MDClass> iterator = list.iterator();
            while (iterator.hasNext()) {
                MDClass mdClass = iterator.next();
                if (mdClass.getParentUrl().equals(parentPath)) {
                    newlist.add(mdClass);
                    tree2(list, mdClass.getUrl());
                }
            }
    
            for (MDClass mdClass : newlist) {
                ArrayList<MDClass> children = new ArrayList<>();
                for (MDClass obj : list) {
                    if (obj.getParentUrl().equals(mdClass.getUrl())) {
                        children.add(obj);
                    }
                }
                mdClass.setChildren(children);
            }
            return newlist;
    
        }
    
        private static void tree(File f, int level, ArrayList<MDClass> list, ArrayList<String> mdSidebar) {
            ArrayList<MDClass> mdClasses = new ArrayList<>();
            File[] childs = f.listFiles();
            for (int i = 0; i < childs.length; i++) {
    
                String space = "";
                for (int i1 = 0; i1 < level-1; i1++) {
                    space += "  ";
                }
    
                File child = childs[i];
                String name = child.getName();
                String url = child.getAbsolutePath().replace(path, "").replace("\\", "/");
                if (child.isDirectory()) {
                    if (!Arrays.asList(excludeDir).contains(child.getName())) {
                        mdSidebar.add(space +  "*  "+child.getName());
                        list.add(new MDClass(Math.random() + "", child.getName(), url, "2", mdClasses, child.getParent().replace(path, "").replace("\\", "/"), child.getName()));
                        tree(child, level + 1, list,  mdSidebar);
                    }
                } else {
                    if (name.endsWith(".md") && !name.startsWith("_sidebar") && !name.startsWith("_navbar")) {
                        String label = child.getName().replace(".md", "");
                        String mdUrl = "*  [" + label + "](" + url + ")";
                        mdSidebar.add(space + mdUrl);
                        list.add(new MDClass(Math.random() + "", label, url, "1", mdClasses, child.getParent().replace(path, "").replace("\\", "/"), mdUrl));
    
                    }
                }
    
            }
        }
    }
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    ![在这里插入图片描述](https://img-blog.csdnimg.cn/884dcdb4e7524800ba720497d638245c.png)
    
    
    • 1
    • 2
  • 相关阅读:
    IDEA断点常用5种方式——条件断点(循环)、回退、表达式执行、中断(不执行后续代码)、指定异常(常用寻找空指针位置)
    Perl文件锁机制:守护你的数据安全
    【shapely】自相交问题
    回顾复习【矩阵分析】初等因子 和 矩阵的相似 || 由不变因子求初等因子 || 由初等因子和秩求Smith标准形(不变因子)
    Python数据分析实战-表连接-merge四种连接方式用法(附源码和实现效果)
    yaml编写规则以及YAML和JSON对比
    图(Graph)
    Editing Conditional Radiance Fields
    让 K8s 更简单!8款你不得不知的 AI 工具-Part 2
    redhawk: create timing window文件
  • 原文地址:https://blog.csdn.net/weixin_44797182/article/details/126871427