yml转properties java工具类
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.*;
public class YmlUtils {
public static void main(String[] args) {
castProperties("test.yml");
}
public static List<YmlNode> castProperties(String ymlFileName) {
if (ymlFileName == null || ymlFileName.isEmpty() || !ymlFileName.endsWith(".yml")) {
throw new RuntimeException("请输入yml文件名称!!");
}
File ymlFile = new File(ymlFileName);
if (!ymlFile.exists()) {
throw new RuntimeException("工程根目录下不存在 " + ymlFileName + "文件!!");
}
String fileName = ymlFileName.split(".yml", 2)[0];
String yml = read(ymlFile);
List<YmlNode> nodeList = getNodeList(yml);
String str = printNodeList(nodeList);
String propertiesName = fileName + ".properties";
File file = new File(propertiesName);
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try (FileWriter writer = new FileWriter(file)) {
writer.write(str);
writer.flush();
} catch (IOException e) {
e.printStackTrace();
}
return nodeList;
}
public static Map<String, String> getPropertiesMap(String ymlFileName) {
Map<String, String> map = new HashMap<>();
List<YmlNode> list = castProperties(ymlFileName);
for (YmlNode node : list) {
if (node.getKey().length() > 0) {
map.put(node.getKey(), node.getValue());
}
}
return map;
}
private static String read(File file) {
if (Objects.isNull(file) || !file.exists()) {
return "";
}
try (FileInputStream fis = new FileInputStream(file)) {
byte[] b = new byte[(int) file.length()];
fis.read(b);
return new String(b, StandardCharsets.UTF_8);
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
private static String printNodeList(List<YmlNode> nodeList) {
StringBuilder sb = new StringBuilder();
for (YmlNode node : nodeList) {
if (node.getLast().equals(Boolean.FALSE)) {
continue;
}
if (node.getEmptyLine().equals(Boolean.TRUE)) {
System.out.println();
sb.append("\r\n");
continue;
}
if (node.getHeadRemark().length() > 0) {
String s = "# " + node.getHeadRemark();
System.out.println(s);
sb.append(s).append("\r\n");
continue;
}
if (node.getTailRemark().length() > 0) {
String s = "# " + node.getTailRemark();
System.out.println(s);
sb.append(s).append("\r\n");
}
String kv = node.getKey() + "=" + node.getValue();
System.out.println(kv);
sb.append(kv).append("\r\n");
}
return sb.toString();
}
private static List<YmlNode> getNodeList(String yml) {
String[] lines = yml.split("\r\n");
List<YmlNode> nodeList = new ArrayList<>();
Map<Integer, String> keyMap = new HashMap<>();
Set<String> keySet = new HashSet<>();
for (String line : lines) {
YmlNode node = getNode(line);
if (node.getKey() != null && node.getKey().length() > 0) {
int level = node.getLevel();
if (level == 0) {
keyMap.clear();
keyMap.put(0, node.getKey());
} else {
int parentLevel = level - 1;
String parentKey = keyMap.get(parentLevel);
String currentKey = parentKey + "." + node.getKey();
keyMap.put(level, currentKey);
node.setKey(currentKey);
}
}
keySet.add(node.getKey() + ".");
nodeList.add(node);
}
for (YmlNode each : nodeList) {
each.setLast(getNodeLast(each.getKey(), keySet));
}
return nodeList;
}
private static boolean getNodeLast(String key, Set<String> keySet) {
if (key.isEmpty()) {
return true;
}
key = key + ".";
int count = 0;
for (String each : keySet) {
if (each.startsWith(key)) {
count++;
}
}
return count == 1;
}
private static YmlNode getNode(String line) {
YmlNode node = new YmlNode();
node.setEffective(Boolean.FALSE);
node.setEmptyLine(Boolean.FALSE);
node.setHeadRemark("");
node.setKey("");
node.setValue("");
node.setTailRemark("");
node.setLast(Boolean.FALSE);
node.setLevel(0);
String trimStr = line.trim();
if (trimStr.isEmpty()) {
node.setEmptyLine(Boolean.TRUE);
return node;
}
if (trimStr.startsWith("#")) {
node.setHeadRemark(trimStr.replaceFirst("#", "").trim());
return node;
}
String[] strs = line.split(":", 2);
if (strs.length == 0) {
return node;
}
node.setKey(strs[0].trim());
String value;
if (strs.length == 2) {
value = strs[1];
} else {
value = "";
}
String tailRemark = "";
if (value.contains(" #")) {
String[] vs = value.split("#", 2);
if (vs.length == 2) {
value = vs[0];
tailRemark = vs[1];
}
}
node.setTailRemark(tailRemark.trim());
node.setValue(value.trim());
int level = getNodeLevel(line);
node.setLevel(level);
node.setEffective(Boolean.TRUE);
return node;
}
private static int getNodeLevel(String line) {
if (line.trim().isEmpty()) {
return 0;
}
char[] chars = line.toCharArray();
int count = 0;
for (char c : chars) {
if (c != ' ') {
break;
}
count++;
}
return count / 2;
}
}
class YmlNode {
private Integer level;
private String key;
private String value;
private Boolean emptyLine;
private Boolean effective;
private String headRemark;
private String tailRemark;
private Boolean last;
public Boolean getLast() {
return last;
}
public void setLast(Boolean last) {
this.last = last;
}
public Integer getLevel() {
return level;
}
public void setLevel(Integer level) {
this.level = level;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public Boolean getEmptyLine() {
return emptyLine;
}
public void setEmptyLine(Boolean emptyLine) {
this.emptyLine = emptyLine;
}
public Boolean getEffective() {
return effective;
}
public void setEffective(Boolean effective) {
this.effective = effective;
}
public String getHeadRemark() {
return headRemark;
}
public void setHeadRemark(String headRemark) {
this.headRemark = headRemark;
}
public String getTailRemark() {
return tailRemark;
}
public void setTailRemark(String tailRemark) {
this.tailRemark = tailRemark;
}
}
原文