JAVA读取yml配置⽂件指定key下的所有内容先引⼊需要的依赖
<!--读取yml⽂件-->
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.23</version>
</dependency>
读取YML⽂件⼯具类的代码
import org.apachemons.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 = Logger(YmlUtils.class);
private static String bootstrap_file = "l";
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, keys) {
result = new HashMap<>();
if(filePath == null) filePath = bootstrap_file;
spring怎么读取配置InputStream in = null;
try {
File file = File(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> Set()){
String key = Key();
Object val = Value();
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, String());
}
}
return result;
} catch (FileNotFoundException e) {
<(e.getMessage(),e);
}finally {
if (in != null){
try {
in.close();
} catch (IOException e) {
<(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;
(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, keys){ for(Map.Entry<String,Object> Set()){
String key = Key();
Object val = Value();
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_String());
}
}
return result;
}
/**
* 获取l的name
* @return
*/
public static String getApplicationName() throws FileNotFoundException {
return getYmlByFileName(bootstrap_file).get("server.port");
}
/**
* 获取l的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 = Set();
for (Map.Entry<String, String> entry : entries) {
System.out.Key()+"==="+Value());
}
System.out.println(getApplicationName());
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。