您的位置:首页 > 其它

如何读取yaml(yml)文件

2018-01-16 11:11 399 查看
public class Config {
private static final Config CONFIG = new Config("/codes.yml");

private Map<String, Object> root;

private Config(String... fileNames) {
Map<String, Object> map = new HashMap<String, Object>(0);
for (String fileName : fileNames) {
Map<String, Object> conf = this.getRoot(fileName);
map = this.marge(map, conf);
}
root = map;
}

/**
*
* @return
*/
public static Config getConfig() {
return CONFIG;
}

/**
* @param key
* @return
*/
private Object getValue(String key) {
Object value = root.get(key);

if (value == null) {
throw new RuntimeException(String.format(
"The value corresponding to the key is not set.key[%s]",
key));
}

return value;
}

/**
*
* @param key
* @return
*/
public String get(String key) {
Object value = getValue(key);
return value.toString();
}

public Object getOj(String key) {
Object value = getValue(key);
return value;
}

/**
* @param resouce
* @return
*/
@SuppressWarnings("unchecked")
private Map<String, Object> getRoot(String resouce) {
InputStream in = null;
try {
in = Config.class.getResourceAsStream(resouce);
return (Map<String, Object>) Yaml.load(in);
} catch (Exception err) {
throw new RuntimeException(
"It failed in reading the configuration file.", err);
}
}

/**
* @param map1
* @param map2
* @return
*/
private Map<String, Object> marge(Map<String, Object> map1,
Map<String, Object> map2) {

Map<String, Object> marged = new HashMap<String, Object>(map1);

for (String key : map2.keySet()) {
if (marged.containsKey(key)) {
throw new RuntimeException(String.format(
"The key overlaps. [%s]", key));
}
marged.put(key, map2.get(key));
}

return marged;
}
}

调用:

Config.getConfig().get("key")
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: