【Java】自定义实现YAML读取工具类

该代码示例展示了如何在Java中使用snakeyaml库加载和解析YAML文件,包括从类路径读取文件,获取配置项的值,并提供了关闭输入流的方法。

1、加载pom

<dependency>
    <groupId>org.yaml</groupId>
    <artifactId>snakeyaml</artifactId>
    <version>1.31</version>
</dependency>

2、代码

package com.tools.util;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.yaml.snakeyaml.Yaml;

import java.io.Closeable;
import java.io.InputStream;
import java.util.Map;
import java.util.Objects;

/**
 * @Author: wolfengi
 * @Date: 2022/7/20 17:48
 * @FileName: YamlUtils
 * @Description:
 */
@Slf4j
public class YamlUtils {
    /**
     * 加载yml文件
     *
     * @param yamlFilePath
     * @return
     */
    public static Map<String, Object> get(String yamlFilePath) {
        Yaml yaml = new Yaml();
        Map<String, Object> map = yaml.load(getInputStream(yamlFilePath));
        return map;
    }

    /**
     * 加载yml文件
     *
     * @param ins
     * @return
     */
    public static Map<String, Object> get(InputStream ins) {
        Yaml yaml = new Yaml();
        Map<String, Object> map = yaml.load(ins);
        return map;
    }

    /**
     * 获取自定义配置 key 的 value <br/>
     * 自定义yml配置文件名称
     *
     * @param yamlFileName yml文件名称
     * @param keys         这是一个 String 类型的 可变参数,传入yaml配置的key值,依层级顺序
     * @return
     */
    public static Object getValue(String yamlFileName, String... keys) {
        Object obj = get(yamlFileName);
        if (StrUtil.isAllEmpty(keys)) {
            return obj;
        }
        for (String key : keys) {
            if (obj instanceof Map) {
                obj = ((Map<String, Object>) obj).get(key);
            } else {
                return obj;
            }
        }
        return obj;
    }

    /**
     * 关闭流
     *
     * @param closeable
     */
    public static void close(Closeable closeable) {
        if (null != closeable) {
            try {
                closeable.close();
            } catch (Exception e) {
            }
        }
    }

    /**
     * 兼容分模块开发中的配置文件加载
     *
     * @param yamlFilePath
     * @return
     */
    private static InputStream getInputStream(String yamlFilePath) {
        InputStream ins = null;
        try {
            ins = Thread.currentThread().getContextClassLoader().getResourceAsStream(yamlFilePath);
            if (null == ins) {
                log.warn(">>>>>>>>> Thread.currentThread().getContextClassLoader().getResourceAsStream({}) is null !", yamlFilePath);
                ins = YamlUtils.class.getClassLoader().getResourceAsStream(yamlFilePath);
                if (null == ins) {
                    log.warn(">>>>>>>>> YamlUtils.class.getClassLoader().getResourceAsStream({}) is null !", yamlFilePath);
                    ins = Objects.requireNonNull(YamlUtils.class.getResourceAsStream(yamlFilePath));
                }
            }
        } catch (Exception e) {
            log.error("[x] File not found ! {}", yamlFilePath, e);
        } finally {
            close(ins);
        }
        return ins;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值