当前位置: 首页 > news >正文

【使用apache snakeyaml 管理yml文件】

使用apache snakeyaml 管理yml文件

  • 1. 一个Yaml工厂
  • 2.Yaml工具类
  • 3. 测试类
  • 4. 完成 ! OK

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() {@Overrideprotected 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

http://www.lryc.cn/news/257531.html

相关文章:

  • uniapp - 简单版本自定义tab栏切换
  • 工作中常用的RabbitMQ实践
  • 程序员常用英文单词
  • QStringListModel 是 Qt 框架中用于在模型-视图(Model-View)架构中展示字符串列表的类
  • vue使用echarts显示中国地图
  • ATM的转账
  • 在Windows 10或11中,复制和粘贴不起作用,不一定是键盘的问题
  • git 使用记录
  • 支持大模型训练的计算机系统
  • SAP ABAP-AVL-OO方法中的ALV的如何自己添加按钮及其响应
  • uniapp移动端悬浮按钮(吸附边缘)
  • 【成功】Linux安装Mysql8并设置远程连接
  • 高效纯化树脂A-2313 CPR
  • uni-app实现安卓原生态调用身份证阅读器读卡库读身份证和社保卡、银行卡、IC卡等功能
  • 【QT】QComboBox和QPlainTextEdit基本介绍和应用示例
  • Path的使用-path绘制折线的时候带上圆角
  • Go Fyne 入门
  • 如何为游戏角色3D模型设置纹理贴图
  • 2024 年 SEO 现状
  • Mac虚拟机CrossOver23破解版下载和许可证下载
  • 宝藏级实用工具,制作电子书轻而易举
  • 虚拟局域网(VLAN)解析(Virtual Local Area Network)(用于在不受物理位置限制的情况下将设备划分到同一网络或不同网络)
  • uni-app 微信小程序之好看的ui登录页面(四)
  • 解决火狐浏览器拖拽事件打开新页面的问题
  • 以为回调函数是同步的(js的问题)
  • 如何在小米路由器4A千兆版刷入OpenWRT并通过内网穿透工具实现公网远程访问
  • diffusers pipeline拆解:理解pipelines、models和schedulers
  • Spring 装配Bean详解
  • udp多播组播
  • 逆向修改Unity的安卓包资源并重新打包