以下是操作yaml代码
pom文件引这个
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>2.11.1</version>
</dependency>
下边是代码,传入yml 文件的路径就行(亲测,不知yml可以,yaml等键值对的文件都可以操作)
private static DumperOptions dumperOptions = new DumperOptions();
static {
dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
dumperOptions.setDefaultScalarStyle(DumperOptions.ScalarStyle.PLAIN);
dumperOptions.setPrettyFlow(false);
}
/**
* yaml文件转为map
* @param yamlUr yaml文件的路径
* @return map
*/
public static Map updateYaml(String yamlUr) {
Yaml yaml = new Yaml(dumperOptions);
Map map = null;
try {
//将yaml文件加载为map格式
map = yaml.loadAs(new FileInputStream(yamlUr), Map.class);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return map;
}
最后,贴上一段之前所用到的代码,一个完整的类,可以直接引进去用
package com.example.demo.utils; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.yaml.snakeyaml.DumperOptions; import org.yaml.snakeyaml.Yaml; import java.io.FileNotFoundException; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; import java.util.Objects; /** * @author cmf * @date 2023/5/29 */ public class YamlUtil { private static final Logger log = LoggerFactory.getLogger(YamlUtil.class); /** * 读取yml文件配置 * * @param fileName 文件的路径 * @return yaml文件转Map(hashmap) * @throws FileNotFoundException 1.文件不存在 2. 文件没有读写权限 */ public static Map<?, ?> loadYaml(String fileName) throws FileNotFoundException { InputStream in = YamlUtil.class.getClassLoader().getResourceAsStream(fileName); return StringUtils.isNotEmpty(fileName) ? (LinkedHashMap<?, ?>) new Yaml().load(in) : null; } /** * 写回yaml * * @param fileName * @param map * @throws IOException */ public static void dumpYaml(String fileName, Map<?, ?> map) throws IOException { if (StringUtils.isNotEmpty(fileName)) { FileWriter fileWriter = new FileWriter(Objects.requireNonNull(YamlUtil.class.getResource(fileName)).getFile()); DumperOptions options = new DumperOptions(); options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); Yaml yaml = new Yaml(options); yaml.dump(map, fileWriter); } } /** * 获取指定key的值 * * @param key 要获取的值对应的key的名称 * @param fileName 文件存放路径 * @return 对应key的值(String) */ public static String getConfig(String key, String fileName) { HashMap<String, String> map = null; try { map = (HashMap<String, String>) loadYaml(fileName); } catch (FileNotFoundException e) { return null; } String value = map.get(key); if (value == null) { Map<?, ?> yamlMap = null; try { yamlMap = YamlUtil.loadYaml(fileName); value = String.valueOf(YamlUtil.getProperty(yamlMap, key)); map.put(key, value != null ? value : StringUtils.EMPTY); } catch (FileNotFoundException ignored) { log.error("获取全局配置异常 {}", key); } } return value; } /** * 获取指定key的值 * * @param map yml配置文件,可以调34行的loadYaml()方法获取 * @param qualifiedKey key * @return 对应key的值(Object) */ public static Object getProperty(Map<?, ?> map, Object qualifiedKey) { if (map != null && !map.isEmpty() && qualifiedKey != null) { String input = String.valueOf(qualifiedKey); if (!input.equals("")) { if (input.contains(".")) { int index = input.indexOf("."); String left = input.substring(0, index); String right = input.substring(index + 1, input.length()); return getProperty((Map<?, ?>) map.get(left), right); } else if (map.containsKey(input)) { return map.get(input); } else { return null; } } } return null; } @SuppressWarnings("unchecked") public static void setProperty(Map<?, ?> map, Object qualifiedKey, Object value) { if (map != null && !map.isEmpty() && qualifiedKey != null) { String input = String.valueOf(qualifiedKey); if (!input.equals("")) { if (input.contains(".")) { int index = input.indexOf("."); String left = input.substring(0, index); String right = input.substring(index + 1, input.length()); setProperty((Map<?, ?>) map.get(left), right, value); } else { ((Map<Object, Object>) map).put(qualifiedKey, value); } } } } }
————————————————
版权声明:本文为CSDN博主「xiaoan9527」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/xiaoan9527/article/details/119649937