properties文件转yaml格式

 /**
     * 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;
    }

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值