zoukankan      html  css  js  c++  java
  • MD文件利用标题等级进行分割代码实现

    MD文件分割

    注意事项:

    1、在DM的文件中,在要进行分割的标题行中不可以有以下转义字符:

    \  /  
    

    2、输出文件中会有多出一个null .md 文件删掉就好了,不影响操作。

    3、注意参数

    package com.dyaqi.demo01;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    
    /**
     * @author Dyaqi
     * @date 2020/7/10 14:52
     * @功能描述:
     */
    public class Test01 {
    
        public static void main(String[] args) {
            segmentation("D:\WorkSpace\BC\MyWorkSpace接口文档.md","D:\WorkSpace\BC\接口文档\", true, "##");
        }
    
        /**
         * 文件切分
         * @param inFilePath  输入文件路径
         * @param outFilePath 输出文件路径
         * @param isTrue      是否换行
         * @param tittleLevel 标题等级  就是 几个 ##
         */
        public static void segmentation(String inFilePath, String outFilePath, Boolean isTrue, String tittleLevel) {
            String contains = null;
            if (isTrue) {
                contains = "
    " + tittleLevel + " ";
            } else {
                contains = tittleLevel + " ";
            }
            try {
                FileReader read = new FileReader(inFilePath);
                BufferedReader br = new BufferedReader(read);
    
                // 行字符串
                String row;
                StringBuilder row2;
                // 存储两行的数据,即上一行的和本行的,按照原文进行存储的(含回车)
                StringBuilder beforeRow = new StringBuilder();
                // 最终结果的文字集(含回车)
                StringBuilder allRow = new StringBuilder();
                // 标题
                String tittle = null;
    			// 文件个数
                int rownum = 0;
    
                while ((row = br.readLine()) != null) {
    
                    row2 = new StringBuilder(row);
    
                    allRow.append("
    ");
                    allRow.append(beforeRow);
    
                    // 储存本行数据
                    beforeRow.append("
    ");
                    beforeRow.append(row2);
    
                    // 判断 beforeRow这两行代码是否 是某一级标题
                    if (beforeRow.toString().contains(contains)) {
                        System.out.println(allRow);
                        writeTxt(outFilePath + tittle+".md",allRow.toString());
                        tittle = row2.substring(3, row2.length());
                        System.err.println("tittle: " + tittle + rownum);
                        allRow = new StringBuilder();
                        rownum++;
                    }
    
                    beforeRow = new StringBuilder();
                    // 在清空之后 据储存上一行数据
                    beforeRow = row2;
    
                }
                System.out.println(allRow);
                writeTxt(outFilePath + tittle+".md",allRow.toString());
                System.out.println("rownum: " + rownum);
            } catch (Exception e) {
                e.printStackTrace();
            }
    
        }
    
        /**
         *  输出文件
         * @param txtPath
         * @param content
         */
        public static void writeTxt(String txtPath,String content){
    
            FileOutputStream fileOutputStream = null;
            File file = new File(txtPath);
            try {
                if(file.exists()){
                    //判断文件是否存在,如果不存在就新建一个txt
                    file.createNewFile();
                }
                fileOutputStream = new FileOutputStream(file);
                fileOutputStream.write(content.getBytes());
                fileOutputStream.flush();
                fileOutputStream.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
  • 相关阅读:
    jq绑定事件
    NReco.PdfGenerator HtmlToPdfConverter 使用示例(未完成)
    msbuild 简单命令
    python 文件操作
    OpenXml demo
    js 常见代码
    C# 静态变量、静态函数、实体变量、实体函数在一个类中的执行顺序
    线程相关技术
    css 光标
    ECMAScript面向对象(二)——之创建对象方法总结
  • 原文地址:https://www.cnblogs.com/dyaqi/p/13283269.html
Copyright © 2011-2022 走看看