Map數據生成Yaml文件

Code:java

 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<String, Object> root = new HashMap<String, Object>();
19         Map<String, Object> score = new HashMap<String, Object>();
20         Map<String, Object> classOne = new HashMap<String, Object>();
21         Map<String, Object> classTwo = new HashMap<String, Object>();
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 }

 

運行結果:spa

相關文章
相關標籤/搜索