标签:文件 cep set collect name yaml 赋值 ret yam
JYaml实例:
将Java实体类写入yaml文件
//创建实体类
package com.richfit.example;
import java.util.List;
public class General {
private String name;
private String address;
private List<String> family;
//省略getter和setter
}
package com.richfit.example;
import org.ho.yaml.Yaml;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
public class demo {
//为实体类赋值
public static General set(){
General general=new General();
general.setName("许褚");
general.setAddress("安徽亳州");
List<String> family=new ArrayList<String>();
family.add("父亲");
family.add("母亲");
general.setFamily(family);
return general;
}
//将实体类直接写入yaml文件
public static void main(String[] args) {
File dumpfile = new File("E:\\workspace_java\\JYaml\\src\\com\\richfit\\example\\general_dump.yaml");
General general=set();
try {
Yaml.dump(general, dumpfile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
效果如下:
--- !com.richfit.example.General
address: 安徽亳州
family:
- 父亲
- 母亲
name: 许褚
将yaml文件load到pojo对象
package com.richfit.example;
import org.ho.yaml.Yaml;
import java.io.File;
import java.io.FileNotFoundException;
public class Load {
public static void main(String[] args) {
General general= new General();
try {
general = (General) Yaml.load(new File("E:\\workspace_java\\JYaml\\src\\com\\richfit\\example\\general_dump.yaml"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
System.out.println(general.getName()+" "+general.getAddress()+" "+general.getFamily());
}
}
注意:
JYaml来自如下依赖:
<!-- https://mvnrepository.com/artifact/org.jyaml/jyaml -->
<dependency>
<groupId>org.jyaml</groupId>
<artifactId>jyaml</artifactId>
<version>1.3</version>
</dependency>
可能会出现发自动导入依赖的jar包,可以下载jar包自行导入。
流处理
package com.richfit.example;
import org.ho.yaml.Yaml;
import org.ho.yaml.YamlEncoder;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class Flow {
public static void main(String[] args) {
File dumpfile=new File("E:\\workspace_java\\JYaml\\src\\com\\richfit\\example\\general_dump.yaml");
File dumpfile1=new File("E:\\workspace_java\\JYaml\\src\\com\\richfit\\example\\general1_dump.yaml");
try {
General general=(General) Yaml.loadType(dumpfile,General.class);
YamlEncoder encoder=new YamlEncoder(new FileOutputStream(dumpfile1));
for (int i=0;i<3;i++){
general.setName(general.getName()+i);
encoder.writeObject(general);
encoder.flush();
}
encoder.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
public void read1(String name) {
Query query = new Query(Criteria.where("name").is(name));//条件查询
List<Book> books = this.mongoTemplate.find(query, Book.class);
File dumpfile = new File("E:\\workspace_java\\JYaml\\src\\main\\java\\com\\richfit\\modules\\mongo\\test\\book.yaml");
try {
for (Book book : books) {
Yaml.dump(book, dumpfile);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
标签:文件 cep set collect name yaml 赋值 ret yam
原文地址:https://www.cnblogs.com/adderhuang/p/11524975.html