美文网首页
[Java]把md文件的二级序号转换成代码块

[Java]把md文件的二级序号转换成代码块

作者: 阿怪_9653 | 来源:发表于2019-04-17 11:54 被阅读0次
改之前
改之后
import jdk.nashorn.internal.runtime.regexp.joni.Regex;

import java.io.*;

/**
 * 把md文档的二级序号转换成代码块
 */
public class mdconvert {
    /* 一次读2048byte
    String inFile = "D:\\MySQL.md";
    FileInputStream inputStream = new FileInputStream(inFile);
    BufferedInputStream inputStream = new BufferedInputStream(file);
    byte[] bytes = new byte[2048];
    int n = -1;
    while (( n = inputStream.read(bytes,0,bytes.length)) != -1){
        String str =  new String(bytes,0,n,"UTF-8");
        str = str.replace("->","--");
        System.out.println(str);
    }
    inputStream.close();
    */
    public static void main(String[] args){
        String inFile = "D:\\MySQL.md";
        String outFile = "D:\\MySQL_new.md";
        FileInputStream inputStream = null;
        InputStreamReader reader = null;
        BufferedReader in = null;
        BufferedWriter out = null;
        try {
            inputStream = new FileInputStream(inFile);
            reader = new InputStreamReader(inputStream,"UTF-8");
            in = new BufferedReader(reader);

            File file = new File(outFile);
            if (!file.exists()){
                file.createNewFile();
            }
            out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile),"UTF-8"));
            String str = null;
            String strAll = "```mysql\n";
            while ((str = in.readLine()) != null ){
                while (str.contains(" * SELECT") || str.contains(" - SELECT ")) {
                    str = str.replaceAll("[-|\\*]\\sSELECT", "SELECT").replaceAll("-[\\s]*>", " -- ").replaceAll("[\\s]+--[\\s]+", "  --  ");
                    strAll +=  str+"\n";
                    str = in.readLine();
                }
                if (!strAll.equals("```mysql\n")){
                    out.write(strAll);
                    out.write("\n");
                    out.write("```\n");
                    out.write("\n");
                    strAll = "```mysql\n";
                }
                out.write(str);
                out.write("\n");
            }
        }catch (FileNotFoundException e){
            e.printStackTrace();
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            try {
                in.close();
                out.flush();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }


    }
}

相关文章

网友评论

      本文标题:[Java]把md文件的二级序号转换成代码块

      本文链接:https://www.haomeiwen.com/subject/fzegwqtx.html