【使用apache snakeyaml 管理yml文件】

1. 一个Yaml工厂

public class YamlFactory {
    public static YamlUtil get(){
        return new YamlUtil();
    }
}

2.Yaml工具类

@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. 测试类

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

  • 6
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值