/**
* properties 格式转化为 yaml 格式字符串
* @param list
* @return
*/
public static String propertiesToYamlString(List<PropertiesKeyValueBean> list){
Properties properties = new Properties();
for (int i = 0; i<list.size();i++){
PropertiesKeyValueBean pro = list.get(i);
properties.setProperty(pro.getProKey(),pro.getProValue());
}
//properties转map
Map<String, Object> map = prop2YmlString(properties);
Yaml yaml3 = new Yaml();
//将Map转换成 YAML 字符串
String yamlStr = yaml3.dumpAsMap(map).replace("'","");
System.out.println(yamlStr);
return yamlStr;
}
/**
* properties 格式转化为 map 格式
* @param properties
* @return
*/
public static Map<String, Object> prop2YmlString(Properties properties){
Map<String, Object> map = new HashMap<>();
if (properties == null || properties.isEmpty()) {
return map;
}
map = prop2Map(properties);
return map;
}
/**
* properties 配置文件转Map 集合
* @param properties
* @return
*/
public static Map<String, Object> prop2Map(Properties properties) {
Map<String,Object> resultMap = new HashMap<>();
try {
Map<String,Object> propMap = new HashMap<>();
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
propMap.put((String) entry.getKey(), entry.getValue());
}
resultMap = parseToMap(propMap);
} catch (Exception e) {
e.printStackTrace();
}
return resultMap;
}
/**
* 将propMap 转化为 HashTable结构
* @param propMap
* @return
*/
private static Map<String, Object> parseToMap(Map<String, Object> propMap) {
Map<String,Object> resultMap = new Hashtable<>();
try {
if (CollectionUtils.isEmpty(propMap)) {
return resultMap;
}
Iterator<Map.Entry<String, Object>> it = propMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, Object> entry = it.next();
String key = entry.getKey();
Object propValue = entry.getValue();
Object resultObj = null;
if (!key.contains(".")) {
if (!key.contains("[") && !key.contains("]")) {
resultMap.put(key,propValue);
}else if (key.contains("[") && key.contains("]")) {
key = key.substring(0,key.lastIndexOf("["));
resultObj = resultMap.get(key);
List<String> list = null;
if (resultObj != null) {
list = (List<String>) resultObj;
}else {
list = new ArrayList<>();
resultMap.put(key,list);
}
list.add((String) propValue);
}
it.remove();
}else {
String currentKey = key.substring(0,key.indexOf("."));
Map<String,Object> childMap = getChildMap(propMap,currentKey);
Map<String,Object> map = parseToMap(childMap);
resultMap.put(currentKey,map);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return resultMap;
}
/**
* 获取拥有相同父级节点的子节点
* @param propMap
* @param currentKey
* @return
*/
private static Map<String, Object> getChildMap(Map<String, Object> propMap, String currentKey) {
Map<String,Object> childMap = new Hashtable<>();
try {
Iterator<Map.Entry<String, Object>> iterator = propMap.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, Object> entry = iterator.next();
String key = entry.getKey();
if (key.contains(currentKey + ".")) {
key = key.substring(key.indexOf(".") + 1);
childMap.put(key,entry.getValue());
// iterator.remove();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return childMap;
}
properties文件转yaml格式
于 2024-04-02 16:24:25 首次发布