如何基于JAVA读取yml配置文件指定key内容
程序员文章站
2022-06-06 15:21:38
这篇文章主要介绍了如何基于java读取yml配置文件指定key内容,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
先引入需要的依赖...
这篇文章主要介绍了如何基于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()); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。