【使用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

相关推荐
执笔论英雄10 小时前
【大模型推理】ScheduleBatch 学习
java·spring boot·学习
CRMEB系统商城10 小时前
【新版发布】标准版PHP v5.6.4正式版,优化部分用户体验
java·大数据·小程序·php·ux
青云交10 小时前
Java 大视界 -- Java 大数据在智慧养老服务需求分析与个性化服务匹配中的应用
java·需求分析·智慧养老·健康管理·java 大数据·个性化服务·生活照料
丈剑走天涯10 小时前
kubernetes 源码编译(ubuntu) kubernetes-1.34.1
java·容器·kubernetes·1024程序员节
今天没ID10 小时前
Java 变量类型转换🙌🙌
java
m0_6740314310 小时前
GitHub等平台形成的开源文化正在重也有人
java·windows·mysql
懒惰蜗牛10 小时前
Day44 | J.U.C中的LockSupport详解
java·开发语言·后端·java-ee
de之梦-御风10 小时前
【工具分享】另一个免费开源的远程桌面服务-Apache Guacamole
开源·apache
5pace11 小时前
Mac Nginx安装、启动、简单命令(苍穹外卖、黑马点评前端环境搭建)
java·前端·nginx·macos·tomcat
库库83911 小时前
Spring AI 知识点总结
java·人工智能·spring