Map数据生成Yaml文件

Code:

 1 package com.lichengbei.j2se;
 2 
 3 import org.yaml.snakeyaml.Yaml;
 4 
 5 import java.io.File;
 6 import java.io.FileWriter;
 7 import java.io.IOException;
 8 import java.util.HashMap;
 9 import java.util.Map;
10 
11 import org.yaml.snakeyaml.DumperOptions;
12 
13 public class YamlDemo1 {
14 
15     public static void main(String[] args) throws IOException {
16 
17         // Map数据准备
18         Map root = new HashMap();
19         Map score = new HashMap();
20         Map classOne = new HashMap();
21         Map classTwo = new HashMap();
22         classOne.put("Polar", "96");
23         classOne.put("Jack", "85");
24         classTwo.put("Rose", "83");
25         score.put("classOne", classOne);
26         score.put("classTwo", classTwo);
27         root.put("score", score);
28 
29         // 写入Yaml
30         DumperOptions dumperOptions = new DumperOptions();
31         dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
32         Yaml yaml = new Yaml(dumperOptions);
33         File dumpFile = new File(System.getProperty("user.dir") + "/Folder1/Score.yaml");
34         FileWriter fileWriter = new FileWriter(dumpFile);
35         yaml.dump(root, fileWriter);
36         fileWriter.close();
37 
38     }
39 
40 }

 

运行结果:

Map数据生成Yaml文件_第1张图片

 

你可能感兴趣的:(Map数据生成Yaml文件)