JAVA读取yml配置文件指定key下的所有内容
先引入需要的依赖
<!--读取yml文件-->
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.23</version>
</dependency>
读取YML文件工具类的代码
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.ResourceUtils;
import org.yaml.snakeyaml.Yaml; import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Set; /**
* @author hunmeng
* @create 2020-01-10 20:34
*/
public class YmlUtils { private static final Logger LOGGER = LoggerFactory.getLogger(YmlUtils.class); private static String bootstrap_file = "classpath:application-test.yml"; private static Map<String,String> result = new HashMap<>(); /**
* 根据文件名获取yml的文件内容
* @param filePath
* @param keys 第一个参数对应第一个key,第二个参数对应第二个key 比如spring.name下的所有 就是两个参数、
* getYmlByFileName(bootstrap_file,"spring", "name");
* @return
*/
public static Map<String,String> getYmlByFileName(String filePath, String... keys) {
result = new HashMap<>();
if(filePath == null) filePath = bootstrap_file;
InputStream in = null;
try {
File file = ResourceUtils.getFile(filePath);
in = new BufferedInputStream(new FileInputStream(file));
Yaml props = new Yaml();
Object obj = props.loadAs(in,Map.class);
Map<String,Object> param = (Map<String, Object>) obj; for(Map.Entry<String,Object> entry:param.entrySet()){
String key = entry.getKey();
Object val = entry.getValue();
if (keys.length != 0 && !keys[0].equals(key)){
continue;
}
if(val instanceof Map){
forEachYaml(key,(Map<String, Object>) val, 1, keys);
}else{
result.put(key, val.toString());
}
}
return result;
} catch (FileNotFoundException e) {
LOGGER.error(e.getMessage(),e);
}finally {
if (in != null){
try {
in.close();
} catch (IOException e) {
LOGGER.error(e.getMessage(),e);
}
}
}
return null;
} /**
* 根据key获取值
* @param key
* @return
*/
public static String getValue(String key) throws FileNotFoundException {
Map<String,String> map = getYmlByFileName(null);
if(map==null)return null;
return map.get(key);
} /**
* 遍历yml文件,获取map集合
* @param key_str
* @param obj
* @param i
* @param keys
* @return
*/
public static Map<String,String> forEachYaml(String key_str,Map<String, Object> obj, int i, String... keys){
for(Map.Entry<String,Object> entry:obj.entrySet()){
String key = entry.getKey();
Object val = entry.getValue();
if (keys.length > i && !keys[i].equals(key)){
continue;
}
String str_new = "";
if(StringUtils.isNotEmpty(key_str)){
str_new = key_str+ "."+key;
}else{
str_new = key;
}
if(val instanceof Map){
forEachYaml(str_new,(Map<String, Object>) val, ++i, keys);
i--;
}else{ result.put(str_new,val.toString());
}
} return result;
} /**
* 获取bootstrap.yml的name
* @return
*/
public static String getApplicationName() throws FileNotFoundException {
return getYmlByFileName(bootstrap_file).get("server.port");
} /**
* 获取bootstrap.yml的name
* @return
*/
public static String getApplicationName1() throws FileNotFoundException {
String name = getYmlByFileName(bootstrap_file).get("spring.application.name");
return name + "center";
} public static void main(String[] args) throws FileNotFoundException { Map<String, String> ymlByFileName = getYmlByFileName(bootstrap_file,"spring");
Set<Map.Entry<String, String>> entries = ymlByFileName.entrySet();
for (Map.Entry<String, String> entry : entries) {
System.out.println(entry.getKey()+"==="+entry.getValue());
} System.out.println(getApplicationName());
} }
JAVA读取yml配置文件指定key下的所有内容的更多相关文章
- SpringBoot中如何优雅的读取yml配置文件?
YAML是一种简洁的非标记语言,以数据为中心,使用空白.缩进.分行组织数据,从而使得表示更加简洁易读.本文介绍下YAML的语法和SpringBoot读取该类型配置文件的过程. 本文目录 一.YAML基 ...
- java读取properties配置文件总结
java读取properties配置文件总结 在日常项目开发和学习中,我们不免会经常用到.propeties配置文件,例如数据库c3p0连接池的配置等.而我们经常读取配置文件的方法有以下两种: (1) ...
- C#读取xml文件指定节点下的值
#region 读取xml文件指定节点下的值 XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(result); XmlNode root ...
- java操作office和pdf文件java读取word,excel和pdf文档内容
在平常应用程序中,对office和pdf文档进行读取数据是比较常见的功能,尤其在很多web应用程序中.所以今天我们就简单来看一下Java对word.excel.pdf文件的读取.本篇博客只是讲解简单应 ...
- java读取properties配置文件信息
一.Java Properties类 Java中有个比较重要的类Properties(Java.util.Properties),主要用于读取Java的配置文件,各种语言都有自己所支持的配置文件,配置 ...
- Java 读取 .properties 配置文件的几种方式
Java 开发中,需要将一些易变的配置参数放置再 XML 配置文件或者 properties 配置文件中.然而 XML 配置文件需要通过 DOM 或 SAX 方式解析,而读取 properties 配 ...
- java读取.properties配置文件的几种方法
读取.properties配置文件在实际的开发中使用的很多,总结了一下,有以下几种方法(仅仅是我知道的):一.通过jdk提供的java.util.Properties类.此类继承自java.util. ...
- Java 读取 .properties 配置文件
java 开发中,经常要读取 properties 配置文件,下面介绍几种读取方式: 1.基于 InputStream 读取配置文件 该方式的优点在于可以读取任意路径下的配置文件 Properties ...
- 【转载】java读取.properties配置文件的几种方法
读取.properties配置文件在实际的开发中使用的很多,总结了一下,有以下几种方法(仅仅是我知道的):一.通过jdk提供的java.util.Properties类.此类继承自java.util. ...
随机推荐
- .net Framework 源代码 · ScrollViewer
本文是分析 .net Framework 源代码的系列,主要告诉大家微软做 ScrollViewer 的思路,分析很简单. 看完本文,可以学会如何写一个 ScrollViewer ,如何定义一个 IS ...
- 简单的Spring Batch示例
使用Spring Batch做为批处理框架,可以完成常规的数据量不是特别大的离线计算. 现在写一个简单的入门版示例. 这里默认大家已经掌握了Spring Batch的基本知识,示例只是为了快速上手实践 ...
- 2018-8-10-win10-uwp-使用-Geometry-resources-在-xaml
title author date CreateTime categories win10 uwp 使用 Geometry resources 在 xaml lindexi 2018-08-10 19 ...
- POI 导入、导出Excel
POI,全称Apache POI,是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能.项目地址:Apache POI - t ...
- Python--day60--一个简单(不完整)的web框架
- P1109 桃花岛
题目描述 不是任何人都可以进入桃花岛的,黄药师最讨厌象郭靖一样呆头呆脑的人.所以,他在桃花岛的唯一入口处修了一条小路,这条小路全部用正方形瓷砖铺设而成.有的瓷砖可以踩,我们认为是安全的,而有的瓷砖一踩 ...
- python面向对象之三大特性
继承 先看个简单的例子了解一下继承. class Animal: # 父类 def __init__(self, name, age, department): self.name = name se ...
- P1027 三角形的周长
题目描述 有n根棍子,棍子i的长度为Ai.现在想要从中选出3根棍子组成周长尽可能长的三角形.请输出最大周长,若无法组成三角形则输出0. 输入格式 第一行是一个正整数n(3<=n<=1000 ...
- 2018-6-15-win10-uwp-xaml-绑定接口
title author date CreateTime categories win10 uwp xaml 绑定接口 lindexi 2018-6-15 21:7:19 +0800 2018-2-1 ...
- vue项目上滑滚动加载更多&下拉刷新
上滑滚动时获取内容高度.屏幕高度和滚动高度(此处#sslist要为内容是id) 内容高度 let innerHeight = document.querySelector("#sslist ...