Java读取/写入Yaml配置文件
来源:互联网 发布:说说人工智能喜马拉雅 编辑:程序博客网 时间:2024/05/18 18:55
JYaml文件流读取/写入Yaml配置文件
yaml配置文件格式规范:- 表示sequence(list列表结构),: 表示map键值对
#以下是示例yaml结构age: 23children: - age: 8 name: mary1 sex: man - age: 9 name: simon2 sex: fatelname: simon.zhangsex: 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.DriverJDBC.url: "jdbc:mysql://127.0.0.1:3306/shipping_local?autoReconnect=true&autoReconnectForPools=true&useUnicode=true&characterEncoding=utf8"JDBC.username: shippingJDBC.password: shippingJDBC.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>
0 0
- Java读取/写入Yaml配置文件
- 使用SnakeYAML读取yaml配置文件
- Java读取Yaml文件
- java 读取和写入配置文件中的数据
- java 有顺序写入读取Properties配置文件
- java Properties配置文件写入与读取
- C++读取、写入配置文件
- 读取和写入配置文件
- 读取配置文件和写入配置文件
- Java配置文件Properties的读取、写入与更新操作
- 对Java配置文件Properties的读取、写入与更新操作
- Java配置文件Properties的读取、写入与更新操作
- 对Java配置文件Properties的读取、写入与更新操作
- 【Java编程】写入、读取、遍历配置文件 Properties类
- Java配置文件Properties的读取、写入与更新操作
- Java配置文件Properties的读取、写入与更新操作
- Java配置文件Properties的读取、写入与更新操作
- Java开发基础——配置文件的写入与读取
- 开博第一篇,新的征程...
- MIT6.828 HW8 uthreads
- Java JDK1.8新特性Lambda 表达式
- linux网络编程之poll
- Timestamp 插入数据库问题
- Java读取/写入Yaml配置文件
- PAT乙级—1010. 一元多项式求导 (25)-native
- leetcode 29. Divide Two Integers
- React Native 植入原生应用
- PHP 批量插入数据三种方法性能比较
- Centos系统备份与恢复教程
- iOS -关于Xcode中的单元测试unitTest的使用
- CSS从一个png图片里取出(截取)里面的小图标
- 【光模块、光接口及光纤知识】