【发布时间】:2019-11-22 13:15:54
【问题描述】:
我想更新(不覆盖)我现有的 yaml 文件: YAML 文件如下所示:
firstName: "John"
#text here
lastName: "Doe"
age: 20
#This is important
city: "XYZ"
我有java类:
ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
String path = "E:/customer.yml";
Customer customer = objectMapper.readValue(new File(path), Customer.class);
customer.setAge(50);
objectMapper.writeValue(new FileWriter(path), customer);
和POJO模型:
private String firstName;
private String lastName;
private int age;
//getters setters
如何在不覆盖的情况下更新 yaml 文件?实际上,在运行这些类之后,值 Age 被修改,但其他属性如 city 被删除。我知道,我可以更新我的 POJO 模型并添加城市,但就我而言,我不确定文件中有什么。 我可以将文件转换为字符串,并替换所需的数据,但它看起来并不专业。
【问题讨论】:
-
这可能会对您有所帮助。 Java - Update the existing Yaml file
-
哲学评论... yaml 是为人类编写和编辑而设计的。所以,如果你需要这个,可能你做错了什么。
标签: java yaml objectmapper