1. 程式人生 > >Java讀取/寫入Yaml配置檔案

Java讀取/寫入Yaml配置檔案

JYaml檔案流讀取/寫入Yaml配置檔案

yaml配置檔案格式規範:- 表示sequence(list列表結構),: 表示map鍵值對
#以下是示例yaml結構

age: 23
children: 
  - 
    age: 8
    name: mary1
    sex: man
  - 
    age: 9
    name: simon2
    sex: fatel
name: simon.zhang
sex: man

1.準備工作,建立Persion實體類

package com.yaml.entity;

import java.util.List;

/**
 * Created by simon on 16-6-12.
 */
public class Person { private String name; private int age; private String Sex; private List<Person> children; //省略 getter and setter method..... }

2.實現讀取/寫入yaml配置檔案

package com.yaml;

import com.yaml.entity.Person;
import org.ho.yaml.Yaml;
import org.junit.Test;

import java.io
.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.List; /** * Hello world! */ public class App { //寫入yaml配置檔案 @Test public void write() { /* Initialize data. */ Person father = new Person(); father.setName("simon.zhang"); father.setAge
(23); father.setSex("man"); List<Person> children=new ArrayList<Person>(); for (int i = 8; i < 10; i++) { Person child = new Person(); if (i % 2 == 0) { child.setSex("man"); child.setName("mary" + (i - 7)); } else { child.setSex("fatel"); child.setName("simon" + (i - 7)); } child.setAge(i); children.add(child); } father.setChildren(children); /* Export data to a YAML file. */ File dumpFile = new File(System.getProperty("user.dir") + "/test/src/main/conf/testYaml.yaml"); try { Yaml.dump(father, dumpFile); } catch (FileNotFoundException e) { e.printStackTrace(); } } //讀取yaml配置檔案 @Test public void read() throws FileNotFoundException { File dumpFile=new File(System.getProperty("user.dir") + "/test/src/main/conf/testYaml.yaml"); Person father = (Person) Yaml.loadType(dumpFile, Person.class); StringBuilder stringBuilder=new StringBuilder(); stringBuilder.append(father.getName()) .append("\t") .append(father.getSex()) .append("\t") .append(father.getAge()) .append("\t") .append(father.getChildren().size()); System.out.println(stringBuilder.toString()); } }
Map結構的yaml檔案
JDBC.driver: com.mysql.jdbc.Driver
JDBC.url: "jdbc:mysql://127.0.0.1:3306/shipping_local?autoReconnect=true&autoReconnectForPools=true&useUnicode=true&characterEncoding=utf8"
JDBC.username: shipping
JDBC.password: shipping
JDBC.autoCommit: false

讀取Map結構

//讀取yaml配置檔案Map結構
    @Test
    public  void  read2() throws FileNotFoundException {
        File dumpFile=new File(System.getProperty("user.dir") + "/test/src/main/conf/testYaml.yaml");
        Map father =Yaml.loadType(dumpFile, HashMap.class);
        for(Object key:father.keySet()){
            System.out.println(key+":\t"+father.get(key).toString());
        }
    }
列印結果如下:

結果

提供 Maven pox.xml 作為參考

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.yaml</groupId>
    <artifactId>test</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>test</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.jyaml</groupId>
            <artifactId>jyaml</artifactId>
            <version>1.3</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>