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 = "\n" + 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("\r\n");
                allRow.append(beforeRow);

                // 储存本行数据
                beforeRow.append("\r\n");
                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();
        }
    }
}
posted @   Dyaqi  阅读(893)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示