yaml文件转properties和properties转yaml
程序员文章站
2022-05-08 10:56:27
...
- 首先要引入依赖
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>2.9.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-properties</artifactId>
<version>2.9.4</version>
</dependency>
- 直接上工具类
package config.center.config.utils;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.dataformat.javaprop.JavaPropsFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
import com.fasterxml.jackson.dataformat.yaml.YAMLParser;
import java.io.*;
import java.nio.charset.Charset;
import java.util.LinkedList;
import java.util.List;
/**
* @program: config
* @description: 转换工具类
* @author: xujingyang
* @create: 2019-03-27 12:16
**/
public class TransferUtils {
private static final String ENCODING = "utf-8";
public static void yml2Properties(String path) {
final String DOT = ".";
List<String> lines = new LinkedList<>();
try {
YAMLFactory yamlFactory = new YAMLFactory();
YAMLParser parser = yamlFactory.createParser(
new InputStreamReader(new FileInputStream(path), Charset.forName(ENCODING)));
String key = "";
String value = null;
JsonToken token = parser.nextToken();
while (token != null) {
if (JsonToken.START_OBJECT.equals(token)) {
// do nothing
} else if (JsonToken.FIELD_NAME.equals(token)) {
if (key.length() > 0) {
key = key + DOT;
}
key = key + parser.getCurrentName();
token = parser.nextToken();
if (JsonToken.START_OBJECT.equals(token)) {
continue;
}
value = parser.getText();
lines.add(key + "=" + value);
int dotOffset = key.lastIndexOf(DOT);
if (dotOffset > 0) {
key = key.substring(0, dotOffset);
}
value = null;
} else if (JsonToken.END_OBJECT.equals(token)) {
int dotOffset = key.lastIndexOf(DOT);
if (dotOffset > 0) {
key = key.substring(0, dotOffset);
} else {
key = "";
lines.add("");
}
}
token = parser.nextToken();
}
parser.close();
System.out.println(lines);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static void properties2Yaml(String path) {
JsonParser parser = null;
JavaPropsFactory factory = new JavaPropsFactory();
try {
parser = factory.createParser(
new InputStreamReader(new FileInputStream(path), Charset.forName(ENCODING)));
} catch (IOException e) {
throw new RuntimeException(e);
}
try {
YAMLFactory yamlFactory = new YAMLFactory();
YAMLGenerator generator = yamlFactory.createGenerator(
new OutputStreamWriter(new FileOutputStream(path), Charset.forName(ENCODING)));
JsonToken token = parser.nextToken();
while (token != null) {
if (JsonToken.START_OBJECT.equals(token)) {
generator.writeStartObject();
} else if (JsonToken.FIELD_NAME.equals(token)) {
generator.writeFieldName(parser.getCurrentName());
} else if (JsonToken.VALUE_STRING.equals(token)) {
generator.writeString(parser.getText());
} else if (JsonToken.END_OBJECT.equals(token)) {
generator.writeEndObject();
}
token = parser.nextToken();
}
parser.close();
generator.flush();
generator.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
转换起来还是很方便的~
推荐阅读
-
SpringBoot基础--yaml配置文件语法和properties配置文件
-
(转)Spring boot 切换配置文件到yaml
-
spring boot 配置文件properties和YAML详解
-
yaml文件转properties和properties转yaml
-
SpringBoot配置文件@PropertySource 同时支持properties文件与yaml(yml)
-
spring Boot yml/yaml 和properties配置文件
-
从spring、spring boot中找到解析properties、xml、yml、yaml文件的方法
-
推荐一个yaml文件转json文件的在线工具 YMALJSONOSOPENSOURCE
-
SpringBoot基础--yaml配置文件语法和properties配置文件