概述
yml文件读入写出:
jar包引入:
https://repo1.maven.org/maven2/org/jyaml/jyaml/1.3/jyaml-1.3.jar
<dependency>
<groupId>org.jyaml</groupId>
<artifactId>jyaml</artifactId>
<version>1.3</version>
</dependency>
读取application.yml文件:
@Test
void test() throws FileNotFoundException {
File dumpFile = new File(System.getProperty("user.dir") + "/src/main/resources/application.yml");
Map<String, Object> configTemp = Yaml.loadType(dumpFile, LinkedHashMap.class);
YamlDecoder dec = new YamlDecoder(new FileInputStream(dumpFile));
while (true) {
try {
configTemp = (Map<String, Object>) dec.readObject();
System.out.println(configTemp);
} catch (EOFException e) {
break;
}
}
}
###写出yml文件:
@Test
public void editInformation() throws Exception {
/* Initialize data. */
Person michael = new Person();
Person floveria = new Person();
Person[] children = new Person[2];
children[0] = new Person();
children[1] = new Person();
michael.setName("Michael Corleone");
michael.setAge(24);
floveria.setName("Floveria Edie");
floveria.setAge(24);
children[0].setName("boy");
children[0].setAge(3);
children[1].setName("girl");
children[1].setAge(1);
michael.setSpouse(floveria);
floveria.setSpouse(michael);
michael.setChildren(children);
floveria.setChildren(children);
/* Export data to a YAML file. */
File dumpFile = new File("John.yaml");
try {
Yaml.dump(michael, dumpFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
实体类
public class Person {
private String name;
private int age;
private Person spouse;
private Person[] children;
public Person(){
}
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
this.age = age;
}
public void setSpouse(Person spouse) {
this.spouse = spouse;
}
public void setChildren(Person[] children) {
this.children = children;
}
public String getName() {
return this.name;
}
public int getAge() {
return this.age;
}
public Person getSpouse() {
return this.spouse;
}
public Person[] getChildren() {
return this.children;
}
}
参考:https://blog.csdn.net/m0_37739193/article/details/78690680
最后
以上就是天真流沙为你收集整理的yml文件读入写出的全部内容,希望文章能够帮你解决yml文件读入写出所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复