YML文件根据文件流一行一行修改数据

目前修改yml格式文件的方法都不会保留原注释,这样改了之后基本没法使用。用文件流的方法,一行一行读取文件,根据文件内容进行修改。

import cn.hutool.core.util.*;

import java.util.*;
import java.io.*;

public class YmlUtils {
    public static void writeYml(String ymlPath, Map jdbcMap) throws IOException {
        BufferedReader br = null;
        PrintWriter pw = null;
        try {
            File file = new File(ymlPath);
            br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
            List list = new ArrayList<>();
            Boolean editDb = false;
            Boolean editRedis = false;
            Boolean editResource = false;
            Boolean editCurrent = false;
            Boolean editServer = false;
            while (true) {
                String str = br.readLine();
                if (str == null) {
                    break;
                }
                if (editServer) {
                    str = "  port: " + jdbcMap.get("currentProjectPort");
                    editServer = false;
                }
                if (editDb) {
                    if (str.contains("url")) {
                        str = "    url: " + jdbcMap.get("jdbcUrl");
                    }
                    if (str.contains("username")) {
                        str = "    username: " + jdbcMap.get("jdbcUserName");
                    }
                    if (str.contains("password")) {
                        str = "    password: " + jdbcMap.get("jdbcPwd");
                        editDb = false;
                    }
                }
                if (editRedis) {
                    if (str.contains("database")) {
                        str = "    database: " + jdbcMap.get("redisBase");
                    }
                    if (str.contains("host")) {
                        str = "    host: " + jdbcMap.get("redisHost");
                    }
                    if (str.contains("port")) {
                        str = "    port: " + jdbcMap.get("redisPort");
                    }
                    if (str.contains("password")) {
                        str = "    password: " + jdbcMap.get("redisPwd");
                        editRedis = false;
                    }
                }
                if (editResource) {
                    final String resourceUrl = URLUtil.normalize(jdbcMap.get("resourceUrl"));
                    str = "  url: " + resourceUrl + "/";
                    editResource = false;
                }
                if (editCurrent) {
                    final String currentProjectUrl = URLUtil.normalize(jdbcMap.get("currentProjectUrl"));
                    str = "  url: " + currentProjectUrl;
                    editCurrent = false;
                }
                if ("server:".equals(str)) {
                    editServer = true;
                }
                if (str.contains("driver-class-name")) {
                    editDb = true;
                }
                if (str.contains("redis:")) {
                    editRedis = true;
                }
                if (str.contains("resource-server")) {
                    editResource = true;
                }
                if (str.contains("current-project")) {
                    editCurrent = true;
                }
                list.add(str);
            }
            pw = new PrintWriter(file, "UTF-8");
            for (int i = 0; i < list.size(); ++i) {
                String str2 = (String) list.get(i);
                pw.println(str2);
            }
        } catch (Exception e) {
            System.out.println("文件==" + ymlPath + "==写入失败");
        } finally {
            if (null != br) {
                br.close();
            }
            if (null != pw) {
                pw.close();
            }
        }
    }
}

你可能感兴趣的:(YML文件根据文件流一行一行修改数据)