yml转换html,properties转换yml格式

springBoot 的配置文件如果用properties会有中文乱码的问题,改成yml可以解决。该工具应运而生。

import com.xiaoleilu.hutool.io.FileUtil;

import org.junit.jupiter.api.Test;

import org.springframework.util.StringUtils;

import java.util.*;

/**

* Created by DimonHo on 2017/11/3.

*/

public class AnalyzerFiler {

/**

* properties转yml

* @param propertiesPath

* @param propertiesCharset

* @param ymlCharset

* @return

*/

public String propertiesToYml(String propertiesPath,String propertiesCharset,String ymlCharset){

List lines = FileUtil.readLines(propertiesPath, propertiesCharset);

String path = FileUtil.getAbsolutePath(propertiesPath);

//使用treemap排好序

Map sourceMap = new TreeMap();

for (String line:lines){

if (!StringUtils.isEmpty(line) && !line.startsWith("#")){

String key = line.substring(0,line.indexOf("="));

String value = line.substring(line.indexOf("=")+1);

sourceMap.put(key,value);

}

}

Iterator it = sourceMap.keySet().iterator();

//保存yml的行内容

List ymlLines = new ArrayList();

//Tab用两个空格格式化

String tab = " ";

//yml文档树

Map> treeMap = new TreeMap>();

//父级名称

String parent = "";

//子节点列表

List element = new ArrayList();

while(it.hasNext()){

String key = it.next();

//.拆分key

String[] keys = key.split("\\.");

String prefix = "";

for (int i=0;i

//从第二个节点开始,行前面需要加tab来格式化,并设置它的父节点

if (i>0){

parent+=keys[i-1];

prefix += tab;

}

String line = prefix + keys[i]+ ": ";

if (treeMap.get(parent)==null) treeMap.put(parent,new ArrayList());

if(!treeMap.get(parent).contains(line)){

element = treeMap.get(parent)==null?new ArrayList():treeMap.get(parent);

if (!element.contains(line)){

element.add(line);

treeMap.put(parent,element);

}

if (i==keys.length-1){

ymlLines.add(line+sourceMap.get(key));

parent = "";

}else{

ymlLines.add(line);

}

}

}

}

FileUtil.writeLines(ymlLines,path.replace(".properties",".yml"),ymlCharset);

return "ok";

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值