博客
关于我
Java解析yaml
阅读量:628 次
发布时间:2019-03-14

本文共 2547 字,大约阅读时间需要 8 分钟。

读取YAML文件并转换为Map的处理流程如下:

代码解释

import java.io.File;import java.io.FileInputStream;import java.util.Iterator;import java.util.Map;import org.yaml.snakeyaml.Yaml;public class YAMLTest {    public static void main(String[] args) throws Exception {        // 读取YAML文件        Yaml yaml = new Yaml();        File file = new File("D:/test.yaml");        Object load = yaml.load(new FileInputStream(file));        // 另一种常见方法:直接读取资源文件        // InputStream io = Thread.currentThread().getContextClassLoader().getResourceAsStream("application.yml");        // Object load = yaml.load(io);        System.out.println("加载结果:\n" + yaml.dump(load));        System.out.println(" ############################################################################# ");        // 将YAML文件转换为Map
Map
map = (Map
) yaml.load(new FileInputStream(file)); System.out.println("转换后的Map:\n" + yaml.dump(map)); System.out.println(" ############################################################################# "); // 遍历Map中的键值对 Iterator
> entryIterator = map.entrySet().iterator(); while (entryIterator.hasNext()) { Map.Entry
entry = entryIterator.next(); String key = entry.getKey(); Object value = entry.getValue(); if (key.equals("ip") || key.equals("port")) { String valueStr = (String) value; System.out.println("键值对:" + key + ":" + valueStr); } else { System.out.println("这是一个嵌套的Map结构,处理方式如下:"); if (key.equals("server")) { Map
subMap = (Map
) value; Iterator
> subEntryIterator = subMap.entrySet().iterator(); while (subEntryIterator.hasNext()) { Map.Entry
subEntry = subEntryIterator.next(); String subKey = subEntry.getKey(); Object subValue = subEntry.getValue(); if (subKey.equals("port2")) { System.out.println("子键:" + subKey + ":" + subValue); } } } else { System.out.println("普通键值对:" + key + ":" + value); } } System.out.println(" ############################################################################# "); } }}

运行结果展示

ip: 192.168.102.31port: 7788spring:  application: {name: cruncherserver: {port2: 9000}}monitor_nic:  nic: {name: ethA, slot: 0, cpu: 0}#############################################################################这一行用于分隔代码输出和解释段键值对:ip: 192.168.102.31键值对:port: 7788这是一个嵌套的Map结构,处理方式如下:普通键值对:spring: {application={name=cruncher}}键值对:server: {port=9000}键值对:monitor_nic: {nic={name=ethA, slot=0, cpu=0}}

Maven依赖说明

org.yaml
snakeyaml
1.26

转载地址:http://ednoz.baihongyu.com/

你可能感兴趣的文章
Memcached:Node.js 高性能缓存解决方案
查看>>
memcache、redis原理对比
查看>>
memset初始化高维数组为-1/0
查看>>
Metasploit CGI网关接口渗透测试实战
查看>>
Metasploit Web服务器渗透测试实战
查看>>
MFC模态对话框和非模态对话框
查看>>
Moment.js常见用法总结
查看>>
MongoDB出现Error parsing command line: unrecognised option ‘--fork‘ 的解决方法
查看>>
mxGraph改变图形大小重置overlay位置
查看>>
MongoDB可视化客户端管理工具之NoSQLbooster4mongo
查看>>
Mongodb学习总结(1)——常用NoSql数据库比较
查看>>
MongoDB学习笔记(8)--索引及优化索引
查看>>
mongodb定时备份数据库
查看>>
mppt算法详解-ChatGPT4o作答
查看>>
mpvue的使用(一)必要的开发环境
查看>>
MQ 重复消费如何解决?
查看>>
mqtt broker服务端
查看>>
MQTT 保留消息
查看>>
MQTT 持久会话与 Clean Session 详解
查看>>
MQTT介绍及与其他协议的比较
查看>>