【使用apache snakeyaml 管理yml文件】

使用apache snakeyaml 管理yml文件

  • [1. 一个Yaml工厂](#1. 一个Yaml工厂)
  • 2.Yaml工具类
  • [3. 测试类](#3. 测试类)
  • [4. 完成 ! OK](#4. 完成 ! OK)

1. 一个Yaml工厂

java 复制代码
public class YamlFactory {
    public static YamlUtil get(){
        return new YamlUtil();
    }
}

2.Yaml工具类

java 复制代码
@Slf4j
public class YamlUtil {

    private Yaml yaml;


    public YamlUtil() {
        yaml = new Yaml(new Representer() {
            @Override
            protected NodeTuple representJavaBeanProperty(
                    Object javaBean, Property property, Object propertyValue,
                    Tag customTag) {
                return propertyValue == null ? null
                        : super.representJavaBeanProperty(javaBean, property, 
                        propertyValue, customTag);
            }
        });
    }

    public <T> T loadAs(String yaml, Class<T> type) {
        return this.yaml.loadAs(yaml, type);
    }

    public boolean write(Object obj, String path) {
        try (Writer writer = this.writer(path)) {
            this.dump(obj, writer);
        } catch (IOException e) {
            log.error("写入文件【{}】失败!", path);
            return false;
        }
        return true;
    }

    public boolean yamlFile(@NonNull Object obj, @NonNull String path) {
        try (Writer writer = this.writer(path)) {
            String yamlStr = this.yaml.dumpAsMap(obj);
            writer.write(yamlStr);
            writer.flush();
            log.info("写入文件: success, 文件路径:【{}】 ", path);
        } catch (IOException e) {
            log.error("写入文件: failed, 文件路径:【{}】 ", path);
            return false;
        }
        return true;
    }

    private void dump(Object obj, Writer writer) {
        this.yaml.dump(obj, writer);
    }

    private Writer writer(String path) {
        try {
            File file = new File(path);
            if (!file.exists()) {
                file.createNewFile();
            }
            return new PrintWriter(file);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

3. 测试类

java 复制代码
public class Test {
    public static void main1(String[] args) throws IOException {
        //String yamlStr = 
        		"name: John\nhobby:\n  - A\n  - B\n  - C\nscores:\n  chinese: 66\n  math: 61";
        //Me me = yaml.loadAs(yamlStr, Me.class);
        //System.out.println(me);
        ArrayList<String> hobby = new ArrayList<>();
        hobby.add("唱歌");
        hobby.add("跳舞");
        hobby.add("跑步");
        hobby.add("画画");
        HashMap<String, String> scores = new HashMap<>();
        scores.put("语文","88");
        scores.put("数学","91");
        scores.put("英语","56");
        ArrayList<Parent> parents = new ArrayList<>();
        parents.add(new Parent("111","mather"));
        parents.add(new Parent("222","father"));
        Me me_ = new Me("John", hobby, scores,parents);

        String filePath = "D:/test.yml";
        YamlFactory.get().yamlFile(me_,filePath);
    }

    public static void main(String[] args) {
        String path = "";
        //YamlFactory.get().yamlFile()
    }
}
@Data
@AllArgsConstructor
class Me{
    public String name;
    public List<String> hobby;
    public Map<String ,String> scores;
    public List<Parent> parents;
}

@Data
@AllArgsConstructor
class Parent {
    public String name;
    public String role;
}

4. 完成 ! OK

相关推荐
楠枬1 分钟前
网页五子棋——匹配模块
java·spring boot·websocket
qq_124987075313 分钟前
Java+SpringBoot+Vue+数据可视化的综合健身管理平台(程序+论文+讲解+安装+调试+售后)
java·开发语言·spring boot·毕业设计
qq_124987075342 分钟前
Java+SpringBoot+Vue+数据可视化的美食餐饮连锁店管理系统
java·spring boot·毕业设计·美食
m0_748248231 小时前
Spring Framework 中文官方文档
java·后端·spring
Vacant Seat1 小时前
矩阵-矩阵置零
java·矩阵·二维数组
先睡1 小时前
Spring MVC的基本概念
java·spring·mvc
m0_748240541 小时前
Springboot项目:使用MockMvc测试get和post接口(含单个和多个请求参数场景)
java·spring boot·后端
CoderCodingNo1 小时前
【GESP】C++二级真题 luogu-b3865, [GESP202309 二级] 小杨的 X 字矩阵
java·c++
暗诺星刻1 小时前
Java 数学函数库
java·数学·函数·计算器·计算
Shuzi_master71 小时前
<02.21>八股文
java·开发语言