Yml转properties文件工具类YmlUtils的详细过程(不用引任何插件和依赖)

目录
  • 【诞生背景】
  • 【Convert YAML and Properties File 插件的不足】
  • 【自写小工具 YmlUtils 实现】
  • 【源码展示】

【诞生背景】

最近在做某配置中心的时候,配置中心采用properties格式进行配置的(如下图)。

而我们工程的项目配置文件是yml格式的(如下图)。

如果人为手动的一条一条,将yml文件中的配置数据,添加到配置中心,难免会消耗大量的人力和精力,况且还容易输入错误。因此,需要一个工具或插件,将 yml 文件的格式,转换为properties文件。

【Convert YAML and Properties File 插件的不足】

IDEA 有一个插件叫 Convert YAML and Properties File, 于是,首先用了一下 这个插件后,发现了,发现这个插件不太友好,具体有以下几点。

比如,现在我们有如下的 yml 配置文件:

我们用插件将它转化为 properties 文件。

下面是转化后的效果:

从这转换后的效果,我们不难发现该插件有以下几点问题:

(1)转化后,原 yml 配置文件消失(如果转出了问题,想看原配置文件,还看不了了);

(2)排序出现混乱,没有按照原 yml 文件数据进行输出(msg相关的配置本来在原yml文件中是第二个配置,转换后却成为了第一个;同理,mybatis的配置本是最后一个,转化后却放在了第二个);

(3)所有注释均不见了(所有相关的注释全都不见了,包括行级注释和末尾注释);

(4)某些值没有进行配置,但转化后,却显示为了 null 字符串(如 msg.sex 的配置);

(5)该插件仅IDEA有,Eclipse中还没有,不能垮开发工具使用;

【自写小工具 YmlUtils 实现】

针对上面 IDEA 插件的不足,于是自己写了一款小工具 YmlUtils(源码在文章结尾处 ),你可以将它放在工程中的任何位置。

现在,我们同样以刚刚的 yml 配置文件为测试模板,来测试下这款小工具。

测试的方法很简单,只需要将 yml 配置文件放在根目录下,然后写个 Test 类,调用里面的 castProperties 方法即可。

YmlUtils.castProperties("application-test.yml");

执行方法后,首先我们可以看到控制台会将 porperties 文件的内容打印到控制台上面:

程序运行完成后,根目录下会多出一个与yml同名的properties文件,该文件就直接拷贝到相应的地方进行使用,而且原文件也并未收到任何损坏和影响。

【源码展示】

最后附上工具类源码,如果对你有用,记得一键三连(支持原创)。

package com.test.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.*;

/**
 * Yaml 配置文件转 Properties 配置文件工具类
 * @author https://zyqok.blog.csdn.net/
 * @since 2021/08/24
 */
public class YmlUtils {

    /**
     * 将 yml 文件转化为 properties 文件
     *
     * @param ymlFileName 工程根目录下(非resources目录)的 yml 文件名称(如:abc.yml)
     * @return List<Node> 每个Nyml 文件中每行对应解析的数据
     */
    public static List<YmlNode> castProperties(String ymlFileName) {
        if (ymlFileName == null || ymlFileName.isEmpty() || !ymlFileName.endsWith(".yml")) {
            throw new RuntimeException("请输入yml文件名称!!");
        }
        File ymlFile = new File(ymlFileName);
        if (!ymlFile.exists()) {
            throw new RuntimeException("工程根目录下不存在 " + ymlFileName + "文件!!");
        }
        String fileName = ymlFileName.split(".yml", 2)[0];
        // 获取文件数据
        String yml = read(ymlFile);
        List<YmlNode> nodeList = getNodeList(yml);
        // 去掉多余数据,并打印
        String str = printNodeList(nodeList);
        // 将数据写入到 properties 文件中
        String propertiesName = fileName + ".properties";
        File file = new File(propertiesName);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try (FileWriter writer = new FileWriter(file)) {
            writer.write(str);
            writer.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return nodeList;
    }

    /**
     * 将yml转化为porperties文件,并获取转化后的键值对
     *
     * @param ymlFileName 工程根目录下的 yml 文件名称
     * @return 转化后的 porperties 文件键值对Map
     */
    public static Map<String, String> getPropertiesMap(String ymlFileName) {
        Map<String, String> map = new HashMap<>();
        List<YmlNode> list = castProperties(ymlFileName);
        for (YmlNode node : list) {
            if (node.getKey().length() > 0) {
                map.put(node.getKey(), node.getValue());
            }
        }
        return map;
    }

    private static String read(File file) {
        if (Objects.isNull(file) || !file.exists()) {
            return "";
        }
        try (FileInputStream fis = new FileInputStream(file)) {
            byte[] b = new byte[(int) file.length()];
            fis.read(b);
            return new String(b, StandardCharsets.UTF_8);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    private static String printNodeList(List<YmlNode> nodeList) {
        StringBuilder sb = new StringBuilder();
        for (YmlNode node : nodeList) {
            if (node.getLast().equals(Boolean.FALSE)) {
                continue;
            }
            if (node.getEmptyLine().equals(Boolean.TRUE)) {
                System.out.println();
                sb.append("\r\n");
                continue;
            }
            // 判断是否有行级注释
            if (node.getHeadRemark().length() > 0) {
                String s = "# " + node.getHeadRemark();
                System.out.println(s);
                sb.append(s).append("\r\n");
                continue;
            }
            // 判断是否有行末注释 (properties中注释不允许末尾注释,故而放在上面)
            if (node.getTailRemark().length() > 0) {
                String s = "# " + node.getTailRemark();
                System.out.println(s);
                sb.append(s).append("\r\n");
            }
            //
            String kv = node.getKey() + "=" + node.getValue();
            System.out.println(kv);
            sb.append(kv).append("\r\n");
        }
        return sb.toString();
    }

    private static List<YmlNode> getNodeList(String yml) {
        String[] lines = yml.split("\r\n");
        List<YmlNode> nodeList = new ArrayList<>();
        Map<Integer, String> keyMap = new HashMap<>();
        Set<String> keySet = new HashSet<>();
        for (String line : lines) {
            YmlNode node = getNode(line);
            if (node.getKey() != null && node.getKey().length() > 0) {
                int level = node.getLevel();
                if (level == 0) {
                    keyMap.clear();
                    keyMap.put(0, node.getKey());
                } else {
                    int parentLevel = level - 1;
                    String parentKey = keyMap.get(parentLevel);
                    String currentKey = parentKey + "." + node.getKey();
                    keyMap.put(level, currentKey);
                    node.setKey(currentKey);
                }
            }
            keySet.add(node.getKey() + ".");
            nodeList.add(node);
        }
        // 标识是否最后一级
        for (YmlNode each : nodeList) {
            each.setLast(getNodeLast(each.getKey(), keySet));
        }
        return nodeList;
    }

    private static boolean getNodeLast(String key, Set<String> keySet) {
        if (key.isEmpty()) {
            return true;
        }
        key = key + ".";
        int count = 0;
        for (String each : keySet) {
            if (each.startsWith(key)) {
                count++;
            }
        }
        return count == 1;
    }

    private static YmlNode getNode(String line) {
        YmlNode node = new YmlNode();
        // 初始化默认数据(防止NPE)
        node.setEffective(Boolean.FALSE);
        node.setEmptyLine(Boolean.FALSE);
        node.setHeadRemark("");
        node.setKey("");
        node.setValue("");
        node.setTailRemark("");
        node.setLast(Boolean.FALSE);
        node.setLevel(0);
        // 空行,不处理
        String trimStr = line.trim();
        if (trimStr.isEmpty()) {
            node.setEmptyLine(Boolean.TRUE);
            return node;
        }
        // 行注释,不处理
        if (trimStr.startsWith("#")) {
            node.setHeadRemark(trimStr.replaceFirst("#", "").trim());
            return node;
        }
        // 处理值
        String[] strs = line.split(":", 2);
        // 拆分后长度为0的,属于异常数据,不做处理
        if (strs.length == 0) {
            return node;
        }
        // 获取键
        node.setKey(strs[0].trim());
        // 获取值
        String value;
        if (strs.length == 2) {
            value = strs[1];
        } else {
            value = "";
        }
        // 获取行末备注
        String tailRemark = "";
        if (value.contains(" #")) {
            String[] vs = value.split("#", 2);
            if (vs.length == 2) {
                value = vs[0];
                tailRemark = vs[1];
            }
        }
        node.setTailRemark(tailRemark.trim());
        node.setValue(value.trim());
        // 获取当前层级
        int level = getNodeLevel(line);
        node.setLevel(level);
        node.setEffective(Boolean.TRUE);
        return node;
    }

    private static int getNodeLevel(String line) {
        if (line.trim().isEmpty()) {
            return 0;
        }
        char[] chars = line.toCharArray();
        int count = 0;
        for (char c : chars) {
            if (c != ' ') {
                break;
            }
            count++;
        }
        return count / 2;
    }
}

class YmlNode {

    /** 层级关系 */
    private Integer level;
    /** 键 */
    private String key;
    /** 值 */
    private String value;
    /** 是否为空行 */
    private Boolean emptyLine;
    /** 当前行是否为有效配置 */
    private Boolean effective;
    /** 头部注释(单行注释) */
    private String headRemark;
    /** 末尾注释 */
    private String tailRemark;
    /** 是否为最后一层配置 */
    private Boolean last;

    public Boolean getLast() {
        return last;
    }

    public void setLast(Boolean last) {
        this.last = last;
    }

    public Integer getLevel() {
        return level;
    }

    public void setLevel(Integer level) {
        this.level = level;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public Boolean getEmptyLine() {
        return emptyLine;
    }

    public void setEmptyLine(Boolean emptyLine) {
        this.emptyLine = emptyLine;
    }

    public Boolean getEffective() {
        return effective;
    }

    public void setEffective(Boolean effective) {
        this.effective = effective;
    }

    public String getHeadRemark() {
        return headRemark;
    }

    public void setHeadRemark(String headRemark) {
        this.headRemark = headRemark;
    }

    public String getTailRemark() {
        return tailRemark;
    }

    public void setTailRemark(String tailRemark) {
        this.tailRemark = tailRemark;
    }

}

到此这篇关于Yml转properties文件工具类YmlUtils(不用引任何插件和依赖)的文章就介绍到这了,更多相关Yml转properties文件工具类YmlUtils内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • 详解application.properties和application.yml文件的区别

    在springboot框架里进行项目开始时,我们在resource文件夹里可以存放配置文件,而格式可以有两种,properties和yml,前者是扁平的k/v格式,而后者是yml的树型结构,我们建议使用后者,因为它的可读性更强,如果现有是properties,也可以转换成yml格式,我们把properies里按.去拆分即可. 一般上来说,当我们创建一个SpringBoot项目时,IDE会默认帮我们创建一个application.properties配置文件.有些朋友习惯把.properties文

  • SpringBoot读取properties或者application.yml配置文件中的数据

    读取application文件 在application.yml或者properties文件中添加: user.address=china user.company=demo user.name=让我康康 1.使用@Value注解读取 直接 代码如下: package im.homeapi.controller; import org.springframework.beans.factory.annotation.Value; import org.omg.CORBA.PUBLIC_MEMBE

  • @Value如何获取yml和properties配置参数

    @Value获取yml和properties配置参数 Yml: #定时任务配置 application: xxl: job: enabled: true admin: addresses: http:///yusp-job-admin/ #127.0.0.1:8080指网关ip:port,yusp-job-admin为调度中心服务名称.通过网关,注册到微服务的/api/server接口,完成注册动作 executor: appname: af_job #执行器名称,要求务必唯一 ip: 10.2

  • 详解Spring Boot加载properties和yml配置文件

    一.系统启动后注入配置 package com.example.config; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import org.springframewo

  • SpringBoot获取yml和properties配置文件的内容

    (一)yml配置文件: pom.xml加入依赖: <!-- 支持 @ConfigurationProperties 注解 --> <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-configuration-processor --> <dependency> <groupId>org.springframework.boot</groupId>

  • Yml转properties文件工具类YmlUtils的详细过程(不用引任何插件和依赖)

    目录 [诞生背景] [Convert YAML and Properties File 插件的不足] [自写小工具 YmlUtils 实现] [源码展示] [诞生背景] 最近在做某配置中心的时候,配置中心采用properties格式进行配置的(如下图). 而我们工程的项目配置文件是yml格式的(如下图). 如果人为手动的一条一条,将yml文件中的配置数据,添加到配置中心,难免会消耗大量的人力和精力,况且还容易输入错误.因此,需要一个工具或插件,将 yml 文件的格式,转换为properties文

  • Spring Boot中优雅的获取yml文件工具类

    如何在spring boot中优雅的获取.yml文件工具类呢 代码如下: package com.common.base.utils.base; import com.common.base.generator.ResourceManager; import org.yaml.snakeyaml.Yaml; import java.io.InputStream; import java.util.HashMap; import java.util.Map; /** * yml文件工具类 */ p

  • 值得分享的超全文件工具类FileUtil

    结合以前的项目开发中遇到的不同的文件操作,在这里基本上提取出了所遇到过的文件操作的工具类. 1 我项目中用到的文件工具类 1 读取raw文件.file文件,drawable文件,asset文件,比如本地的json数据,本地文本等: 如:String result =FileUtil.getString(context,"raw://first.json") 2 读取本地的property文件,并转化为hashMap类型的数据 (simpleProperty2HashMap): 3 将r

  • Java实现的读取资源文件工具类ResourcesUtil实例【可动态更改值的内容】

    本文实例讲述了Java实现的读取资源文件工具类ResourcesUtil.分享给大家供大家参考,具体如下: package com.gcloud.common; import java.io.Serializable; import java.text.MessageFormat; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Locale; impor

  • Java压缩文件工具类ZipUtil使用方法代码示例

    本文实例通过Java的Zip输入输出流实现压缩和解压文件,前一部分代码实现获取文件路径,压缩文件名的更改等,具体如下: package com.utility.zip; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import

  • java常用工具类 IP、File文件工具类

    本文实例为大家分享了java常用工具类的具体实现代码,供大家参考,具体内容如下 IP工具类 package com.jarvis.base.util; import java.io.IOException; import java.io.InputStreamReader; import java.io.LineNumberReader; /** * * * @Title: IpMacUtil.java * @Package com.jarvis.base.util * @Description

  • Android读写文件工具类详解

    本文实例为大家分享了Android读写文件工具类的具体代码,供大家参考,具体内容如下 public class Utils { private static String path1 = Environment.getExternalStorageDirectory().getAbsolutePath(); private static String path2 = Environment.getDownloadCacheDirectory().getAbsolutePath(); privat

  • Java利用POI读写Excel文件工具类

    本文实例为大家分享了Java读写Excel文件工具类的具体代码,供大家参考,具体内容如下 package com.test.app.utils; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Field; import java.lang.reflect.Method; imp

  • Android封装高德地图定位工具类Util的详细步骤

    目录 前提 第一步.去官网创建高德Key 第二步  通过Gradle集成SDK(方便): 第三步    配置参数 第四步   获取定位数据 总结 前提 每次做的项目中或者维护公司之前旧项目的时候,都会用到通过定位来获取经纬度,我们都知道,Android官方也提供了获取经纬度的方法,但是不太好使,所以就用了高德地图的API,不能每次用的时候都要写一堆代码,效率挺低的,于是就想着,封装成一个工具类,方便调用,为以后的项目,不管是管理方面还是查找方面都简洁了不少. 第一步.去官网创建高德Key 官网地

  • java文件操作工具类分享(file文件工具类)

    复制代码 代码如下: import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.Fil

随机推荐