• Java Yml格式转换为Properties


    Yml格式文件转换为Properties格式

    问题引入

    使用在线的yml转换properties, 发现有属性内容漏了,网站地址https://tooltt.com/yaml2properties/
    于是自己动手写个转换工具类,自测过多个 yml 文件,目前没发现遗漏的。

    需要转换的yaml文件如下

    spring:
      application:
        name: xtoon-sys-server
      cloud:
        nacos:
          config:
            server-addr: localhost:8848
            file-extension: yaml
            enabled: true
      boot:
        admin:
          client:
            url: http://localhost:5001
            username: admin
            password: admin
            instance:
              prefer-ip: true
    
    management:
      health:
        redis:
          enabled: false
      endpoint:
        health:
          show-details: always
      endpoints:
        web:
          exposure:
            include: "*"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    在线转换网站转换的结果

    spring.application.name=xtoon-sys-server
    spring.cloud.nacos.config.server-addr=localhost:8848
    spring.cloud.nacos.config.file-extension=yaml
    spring.boot.admin.client.url=http://localhost:5001
    spring.boot.admin.client.username=admin
    spring.boot.admin.client.password=admin
    management.endpoint.health.show-details=always
    management.endpoints.web.exposure.include=*
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    正确的转换结果应该如下

    spring.application.name=xtoon-sys-server
    spring.cloud.nacos.config.server-addr=localhost:8848
    spring.cloud.nacos.config.file-extension=yaml
    spring.cloud.nacos.config.enabled=true
    spring.boot.admin.client.url=http://localhost:5001
    spring.boot.admin.client.username=admin
    spring.boot.admin.client.password=admin
    spring.boot.admin.client.instance.prefer-ip=true
    management.health.redis.enabled=false
    management.endpoint.health.show-details=always
    management.endpoints.web.exposure.include=*
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    在线网站转换结果截图如下
    在这里插入图片描述
    对比原始文本和转换结果,发现少了几个属性

    spring.cloud.nacos.config.enabled=true
    spring.boot.admin.client.instance.prefer-ip=true
    management.health.redis.enabled=false
    
    • 1
    • 2
    • 3

    这几个结果有些特征,value值是boolean类型的。不知道还有没有其它类型的数据会有遗漏的?

    转换代码

    导入yaml读取jar

    <dependency>
          <groupId>org.yamlgroupId>
          <artifactId>snakeyamlartifactId>
          <version>1.33version>
     dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    Java 代码

    package com.scd.tool;
    
    import org.yaml.snakeyaml.Yaml;
    
    import java.io.FileInputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.ArrayList;
    import java.util.LinkedHashMap;
    import java.util.List;
    import java.util.Set;
    
    /**
     * @author James
     */
    public class YamlToProperties {
    
        public static void main(String[] args) {
            Yaml yaml = new Yaml();
            String filePath = "file/yaml/bootstrap.yml";
            try (InputStream inputStream = new FileInputStream(filePath)) {
                Object object = yaml.load(inputStream);
                List<String> resultList = travelRootWithResult(object);
                System.out.println(resultList);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    
        private static List<String> travelRootWithResult(Object object) {
            List<String> resultList = new ArrayList<>();
            if (object instanceof LinkedHashMap) {
                LinkedHashMap map = (LinkedHashMap) object;
                Set<Object> keySet = map.keySet();
                for (Object key : keySet) {
                    List<String> keyList = new ArrayList<>();
                    keyList.add((String) key);
                    travelTreeNode(map.get(key), keyList, resultList);
                }
            }
            return resultList;
        }
        
    
        private static void travelTreeNode(Object obj, List<String> keyList, List<String> resultList) {
            if (obj instanceof LinkedHashMap) {
                LinkedHashMap linkedHashMap = (LinkedHashMap) obj;
                linkedHashMap.forEach((key, value) -> {
                    if (value instanceof LinkedHashMap) {
                        keyList.add((String) key);
                        travelTreeNode(value, keyList, resultList);
                        keyList.remove(keyList.size() - 1);
                    } else {
                        StringBuilder result = new StringBuilder();
                        for (String strKey : keyList) {
                            result.append(strKey).append(".");
                        }
                        result.append(key).append("=").append(value);
                        System.out.println(result);
                        resultList.add(result.toString());
                    }
                });
            } else {
                StringBuilder result = new StringBuilder();
                result.append(keyList.get(0)).append("=").append(obj);
                System.out.println(result);
                resultList.add(result.toString());
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70

    运行结果如下,对比之后发现没有出现遗漏的

    spring.application.name=xtoon-sys-server
    spring.cloud.nacos.config.server-addr=localhost:8848
    spring.cloud.nacos.config.file-extension=yaml
    spring.cloud.nacos.config.enabled=true
    spring.boot.admin.client.url=http://localhost:5001
    spring.boot.admin.client.username=admin
    spring.boot.admin.client.password=admin
    spring.boot.admin.client.instance.prefer-ip=true
    management.health.redis.enabled=false
    management.endpoint.health.show-details=always
    management.endpoints.web.exposure.include=*
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    大家使用的时候只需要改一下filePath

    代码解读

    可以把yml 看成多个树,问题就转换成了遍历树的问题,我们需要获取树的路径以及子节点。树的路径是properties的key, 叶子节点是properties的value

  • 相关阅读:
    模板特化--类和函数
    Direct3D模板缓存
    ME60单板加载故障维护经验
    vins-mono初始化代码分析
    Linux ARM平台开发系列讲解(CAN) 2.14.1 CAN基础协议分析
    Docker搭建nginx
    Docker容器学习笔记(看了狂神视频)
    打开获取需求的大门——用例图绘制指南
    python代码学习——递归函数
    Java核心编程(23)
  • 原文地址:https://blog.csdn.net/modelmd/article/details/127660133