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. ...
随机推荐
- hdu 3982 Harry Potter and J.K.Rowling (半平面交 + 圆与多边形交)
Problem - 3982 题意就是给出一个圆心在原点半径为R的圆形蛋糕,上面有一个cherry,对蛋糕切若干刀,最后要求求出有cherry的那块的面积占整个蛋糕的多少. 做法显而易见,就是一个半平 ...
- jq实现简单手风琴效果
文章地址:https://www.cnblogs.com/sandraryan/ 利用slideUp slideDown动画 <!DOCTYPE html> <html lang=& ...
- css的一些小问题
这是今天整理的笔记一.属性书写顺序: Formatting Model(布局方式.位置) > Box Model(尺寸) > Typographic(文本相关) > Visual(视 ...
- pycharm下的多个python版本共存(二)
上一篇博文介绍了在windows下同时安装python2和python3.而在工作的过程中,我习惯于用pycharm作为IDE.本文将记录如何在pycharm中选择python版本,并给相应的版本安装 ...
- python的if判断
if 判断条件的时候,如果是多个条件一起进行判断,那么就需要逻辑运算符 并且-----------and 或者-----------or 非(取反)----not if 条件1 and 条件2 ...
- 总结thinkphp快捷查询getBy、getField、getFieldBy用法及场景
thinkphp作为国内现阶段最成熟的框架:没有之一: 不得不说是有好些特别方便的方法的: 然而如果初接触thinkphp的时候难免会被搞的有点迷茫: for example这些: getBy get ...
- 整理了一下react16.7.0的webpack模板
基本上react需要方法和依赖的库都引配好了.github地址:https://github.com/qianxiaoning/demo-react16.7.0 欢迎大家star或者fork呀~ te ...
- SpringBoot2.X 项目使用外置绝对路径的配置文件
spring-boot-absolute-config 前言 该工程是为解决应用部署应用时指定配置文件存放位置的问题. SpringBoot项目默认加载以下位置的配置文件: ? 1 2 3 4 cla ...
- Spring Boot 2.x使用Mockito进行测试
在上一篇,项目基本实现了Spring Boot对Mybatis的整合.这篇文章使用Mockito对项目进行测试. 1.使用postmat测试: 2.编写单元测试类,使用mockito进行测试: 3.使 ...
- mysql(8.0.16)安装及使用注意事项
1.安装地址:https://dev.mysql.com/downloads/mysql/ 2.在安装路径:D:\mysql\mysql-8.0.16-winx64(安装时的路径,可自己选择)下面新建 ...