用Java将多个文件合并成markdown

笔记 / 2020-10-28

写在前面

最近在复习C语言,老师发了很多代码。
代码
像这样,因此合法的整理是非常有必要的。

解决方案

换做常人肯定是挨个打开文件然后逐个整理,但是我们作为程序猿,肯定不能这样啊,效率低不说,还十分掉价,于是java就排上用场了。

文件结构分析

因为生成的目标文件是markdown文件,因此我们要有必要对makrdown进行了解。

这里呢我们先简单介绍两个
# 一级标题
## 二级标题
```cpp
// 代码片段
```
随后单独出一篇文章介绍。

乱码问题

本来以为直接合并就好了,结果发现保存的代码是 GBK格式的,而我的java 默认输入UTF-8 因此文件格式统一就显得非常重要。

  • OutputStreamWriter
  • InputStreamReader
            FileOutputStream fileOutputStream = new FileOutputStream("target.md");
            OutputStreamWriter osw = new OutputStreamWriter(fileOutputStream, "UTF-8");

乱码文件解决了就是写代码了

代码

import java.io.*;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * @author luckyFang
 * @date 2020 10 27
 * @desc 多文件合并到 md
 */

public class DirToMd {
    // 遍历文件夹返回合法文件
    public static List<File> readDirToFileList(String path) throws Exception {
        File file = new File(path);
        if (!file.isDirectory()) {
            throw new Exception("目录非法请重试!");
        }
        // 自定义过滤器(lambda)
        File[] files = file.listFiles((f) -> f.getName().contains(".cpp"));
        return Arrays.asList(files);
    }

    // 写出文件
    public static void writeFiles(List<File> list) {
        try {
            // 乱码解决
            FileOutputStream fileOutputStream = new FileOutputStream("target.md");
            OutputStreamWriter osw = new OutputStreamWriter(fileOutputStream, "UTF-8");

            // 缓冲区
            char[] buffer = new char[1024];
            
            // 遍历写出
            for (File file : list) {
                FileInputStream fileInputStream = new FileInputStream(file);
                InputStreamReader isr = new InputStreamReader(fileInputStream, "GBK");
                int len;
                // head
                osw.append("### " + file.getName() + "\n");
                osw.append("```cpp\n");
                while ((len = isr.read(buffer)) != -1) {
                    osw.write(buffer, 0, len);
                }
                osw.append("```\n");
                osw.flush();
                isr.close();
            }
            osw.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception {
        List<File> files = readDirToFileList("C:\\Users\\13186\\Documents\\WeChat Files\\wxid_rh2fal5u4tz621\\FileStorage\\File\\2020-10");
        // 排序
        files.sort(new Comparator<File>() {
            @Override
            public int compare(File file, File t1) {
                int f1, f2;
                try {
                    f1 = Integer.parseInt(file.getName().substring(0, file.getName().indexOf(".")));
                    f2 = Integer.parseInt(t1.getName().substring(0, t1.getName().indexOf(".")));
                } catch (NumberFormatException ex) {
                    return 0;
                }
                return f1 - f2;
            }
        });
        // 函数式编程
        files.forEach(System.out::println);
        System.out.println(files.stream().count());
        writeFiles(files);
    }
}

效果

传送门