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;
    }
}

原文件内容:
在这里插入图片描述
转换后内容:
在这里插入图片描述

发布了148 篇原创文章 · 获赞 18 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_33321609/article/details/104472772