如何用JAVA读取YML配置文件中特定key的值?

2026-05-28 08:411阅读0评论SEO教程
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计864个文字,预计阅读时间需要4分钟。

如何用JAVA读取YML配置文件中特定key的值?

这篇文章主要介绍了如何基于JAVA读取YML配置文件并指定key内容。以下是一个简单的示例代码,通过代码展示如何实现这一功能。

如何用JAVA读取YML配置文件中特定key的值?

javaimport org.yaml.snakeyaml.Yaml;

import java.io.InputStream;import java.util.Map;

public class YmlReader { public static void main(String[] args) { try { Yaml yaml=new Yaml(); InputStream inputStream=YmlReader.class.getClassLoader().getResourceAsStream(config.yml); Map ymlMap=yaml.load(inputStream); Object value=ymlMap.get(key);

System.out.println(Value of 'key': + value); } catch (Exception e) { e.printStackTrace(); } }}

此代码示例适用于对JAVA和YML配置文件有一定了解的开发者。对于需要深入了解或进行更复杂操作的开发者,建议进一步学习相关知识和实践。需要的朋友可以参考以下资源:

1. Java YML处理库:https://github.com.fasterxml.jackson.dataformat/jackson-dataformat-yaml

2.YML配置文件格式:https://yaml.org/

希望这些信息能帮助你更好地理解并实现这一功能。

这篇文章主要介绍了如何基于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()); } }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。

本文共计864个文字,预计阅读时间需要4分钟。

如何用JAVA读取YML配置文件中特定key的值?

这篇文章主要介绍了如何基于JAVA读取YML配置文件并指定key内容。以下是一个简单的示例代码,通过代码展示如何实现这一功能。

如何用JAVA读取YML配置文件中特定key的值?

javaimport org.yaml.snakeyaml.Yaml;

import java.io.InputStream;import java.util.Map;

public class YmlReader { public static void main(String[] args) { try { Yaml yaml=new Yaml(); InputStream inputStream=YmlReader.class.getClassLoader().getResourceAsStream(config.yml); Map ymlMap=yaml.load(inputStream); Object value=ymlMap.get(key);

System.out.println(Value of 'key': + value); } catch (Exception e) { e.printStackTrace(); } }}

此代码示例适用于对JAVA和YML配置文件有一定了解的开发者。对于需要深入了解或进行更复杂操作的开发者,建议进一步学习相关知识和实践。需要的朋友可以参考以下资源:

1. Java YML处理库:https://github.com.fasterxml.jackson.dataformat/jackson-dataformat-yaml

2.YML配置文件格式:https://yaml.org/

希望这些信息能帮助你更好地理解并实现这一功能。

这篇文章主要介绍了如何基于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()); } }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易盾网络。