properties文件快速轉爲yml文件

只是做了簡單的測試,沒有考慮過文件裏有註釋的情況
思想就是先把數據讀到map中,然後再保存到新的文件中

public class Test {
    public static void main(String[] args) throws Exception {
        String path = "C:\\Users\\12483\\Desktop\\test";
        File oldFile = new File(path + "\\abc.proterties");
        File newFile = new File(path + "\\cde.yml");
        copyTo(oldFile, newFile);
    }

    private static void copyTo(File oldFile, File newFile) throws Exception {
        Map<String, Object> map = new HashMap<>();
        try (FileInputStream fileInputStream = new FileInputStream(oldFile);
             InputStreamReader inBuff = new InputStreamReader(fileInputStream);
             BufferedReader bufferedReader = new BufferedReader(inBuff);
             FileOutputStream fileOutputStream = new FileOutputStream(newFile);
             OutputStreamWriter outBuff = new OutputStreamWriter(fileOutputStream);
             BufferedWriter bufferedWriter = new BufferedWriter(outBuff)) {
            String s;
            while ((s = bufferedReader.readLine()) != null) {
                //把數據讀到map中
                addToMap(map, s);
            }
            //把map中數據輸出到文件內
            show(map, bufferedWriter);
        }
    }

    private static void addToMap(Map<String, Object> map, String s) {
        String[] ss = s.split("=");
        String value = ss[1].trim();
        if (ss.length != 2) {
            System.out.println("數據有錯");
        }
        if (ss[0].trim().length() == 0) {
            System.out.println("key不能爲空");
        }
        String[] keys = ss[0].trim().split("\\.");
        Object o = map.get(keys[0]);
        if (o == null) {
            if (keys.length == 1) {
                //長度爲1結束
                map.put(keys[0], value);
                return;
            } else {
                Map<String, Object> soj = new HashMap<>();
                map.put(keys[0], soj);
            }
        }
        if (keys.length == 1) {
            if (map.containsKey(keys[0])) {
                //出現兩次一樣的
                System.out.println("數據重複");
            } else {
                map.put(keys[0], value);
            }
        }
        Map<String, Object> nowMap = map;
        for (int i = 1; i < keys.length - 1; i++) {
            if (nowMap.get(keys[i - 1]) instanceof String) {
                System.out.println("數據錯誤,不能又有子節點又有值");
            }
            nowMap = (HashMap<String, Object>) nowMap.get(keys[i - 1]);
            if (!nowMap.containsKey(keys[i])) {
                Map<String, Object> soj = new HashMap<>();
                nowMap.put(keys[i], soj);
            }
        }
        int last = keys.length - 1;
        nowMap = (HashMap<String, Object>) nowMap.get(keys[last - 1]);
        if (nowMap.containsKey(keys[last])) {
            System.out.println("數據重複");
        } else {
            nowMap.put(keys[last], value);
        }
    }

    private static void show(Map<String, Object> map, BufferedWriter bufferedWriter) throws IOException {
        show(map, 0, bufferedWriter);
    }

    private static void show(Map<String, Object> map, int indent, BufferedWriter bufferedWriter) throws IOException {
        for (Map.Entry<String, Object> entry : map.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            if (value instanceof String) {
                bufferedWriter.write(getStr(indent) + key + ": " + (String) value);
                bufferedWriter.newLine();
            } else if (value instanceof Map) {
                bufferedWriter.write(getStr(indent) + key + ": ");
                bufferedWriter.newLine();
                show((Map) value, indent + 1, bufferedWriter);
            } else {
                System.out.println("出問題");
            }
        }
    }

    public static String[] indentStr;

    static {
        //默認層數爲8層
        resize(8);
    }

    private static String getStr(int indent) {
        //層數不夠時,擴容
        if (indent >= indentStr.length) {
            resize(indent + (indent >> 1));
        }
        return indentStr[indent];
    }

    private static void resize(int len) {
        String[] newstr = new String[len];
        if (indentStr != null) {
            for (int i = 0; i < indentStr.length; i++) {
                newstr[i] = indentStr[i];
            }
        } else {
            newstr[0] = "";
        }
        for (int i = indentStr == null ? 1 : indentStr.length; i < newstr.length; i++) {
            newstr[i] = newstr[i - 1] + "  ";
        }
        indentStr = newstr;
    }
}

原文件內容:
在這裏插入圖片描述
轉換後內容:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章