博客
关于我
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/

你可能感兴趣的文章
NodeJs学习笔记001--npm换源
查看>>
Nodejs教程09:实现一个带接口请求的简单服务器
查看>>
nodejs配置express服务器,运行自动打开浏览器
查看>>
Node入门之创建第一个HelloNode
查看>>
Node出错导致运行崩溃的解决方案
查看>>
node安装及配置之windows版
查看>>
Node提示:error code Z_BUF_ERROR,error error -5,error zlib:unexpected end of file
查看>>
NOIp2005 过河
查看>>
NOPI读取Excel
查看>>
NoSQL&MongoDB
查看>>
NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty()
查看>>
npm install digital envelope routines::unsupported解决方法
查看>>
npm install报错,证书验证失败unable to get local issuer certificate
查看>>
npm install无法生成node_modules的解决方法
查看>>
npm run build 失败Compiler server unexpectedly exited with code: null and signal: SIGBUS
查看>>
npm run build报Cannot find module错误的解决方法
查看>>
npm run build部署到云服务器中的Nginx(图文配置)
查看>>
npm run dev 报错PS ‘vite‘ 不是内部或外部命令,也不是可运行的程序或批处理文件。
查看>>
npm start运行了什么
查看>>
npm WARN deprecated core-js@2.6.12 core-js@<3.3 is no longer maintained and not recommended for usa
查看>>