码迷,mamicode.com
首页 > 编程语言 > 详细

JYaml——>Yaml的Java实现

时间:2019-09-15 23:57:42      阅读:184      评论:0      收藏:0      [点我收藏+]

标签:文件   cep   set   collect   name   yaml   赋值   ret   yam   

JYaml——>Yaml的Java实现

JYaml支持的数据类型:

  • 原始数据类型和封装类(int,java.lang.Integer)
  • JavaBean兼容对象(结构支持)
  • collection(sequence支持)
    • List
    • Set
  • Map(map支持)
  • Arrays(sequence支持)
  • BigInteger和BigDecimal
  • Date

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();
        }
    }
}

mongodb->pojo->yaml

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();
        }
    }

JYaml——>Yaml的Java实现

标签:文件   cep   set   collect   name   yaml   赋值   ret   yam   

原文地址:https://www.cnblogs.com/adderhuang/p/11524975.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!