|
|
|
@@ -1,32 +1,49 @@ |
|
|
|
package com.ruoyi.platform.utils; |
|
|
|
|
|
|
|
import org.yaml.snakeyaml.DumperOptions; |
|
|
|
import org.yaml.snakeyaml.Yaml; |
|
|
|
import org.yaml.snakeyaml.nodes.Node; |
|
|
|
import org.yaml.snakeyaml.representer.Represent; |
|
|
|
import org.yaml.snakeyaml.representer.Representer; |
|
|
|
//import org.yaml.snakeyaml.Yaml; |
|
|
|
|
|
|
|
import java.io.*; |
|
|
|
import java.util.Iterator; |
|
|
|
import java.util.Map; |
|
|
|
import org.yaml.snakeyaml.nodes.Tag; |
|
|
|
|
|
|
|
|
|
|
|
import org.ho.yaml.Yaml; |
|
|
|
|
|
|
|
public class YamlUtils { |
|
|
|
|
|
|
|
/** |
|
|
|
* 将Map对象转换为YAML格式并写入指定路径的文件中 |
|
|
|
* |
|
|
|
* @param data Map对象 |
|
|
|
* @param path 文件路径 |
|
|
|
* @param fileName 文件名 |
|
|
|
* @param data Map对象 |
|
|
|
* @param path 文件路径 |
|
|
|
* @param fileName 文件名 |
|
|
|
*/ |
|
|
|
// public static void generateYamlFile(Map<String, Object> data, String path, String fileName) { |
|
|
|
// DumperOptions options = new DumperOptions(); |
|
|
|
// options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); |
|
|
|
// options.setDefaultScalarStyle(DumperOptions.ScalarStyle.PLAIN); |
|
|
|
// |
|
|
|
// |
|
|
|
// // 创建Yaml实例 |
|
|
|
// Yaml yaml = new Yaml(options); |
|
|
|
// |
|
|
|
// File directory = new File(path); |
|
|
|
// if (!directory.exists()) { |
|
|
|
// boolean isCreated = directory.mkdirs(); |
|
|
|
// if (!isCreated) { |
|
|
|
// throw new RuntimeException("创建路径失败: " + path); |
|
|
|
// } |
|
|
|
// } |
|
|
|
// |
|
|
|
// String fullPath = path + "/" + fileName + ".yaml"; |
|
|
|
// |
|
|
|
// try (FileWriter writer = new FileWriter(fullPath)) { |
|
|
|
// yaml.dump(data, writer); |
|
|
|
// } catch (IOException e) { |
|
|
|
// e.printStackTrace(); |
|
|
|
// } |
|
|
|
// } |
|
|
|
public static void generateYamlFile(Map<String, Object> data, String path, String fileName) { |
|
|
|
DumperOptions options = new DumperOptions(); |
|
|
|
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); |
|
|
|
options.setDefaultScalarStyle(DumperOptions.ScalarStyle.PLAIN); |
|
|
|
|
|
|
|
// 创建Yaml实例 |
|
|
|
Yaml yaml = new Yaml(options); |
|
|
|
|
|
|
|
File directory = new File(path); |
|
|
|
if (!directory.exists()) { |
|
|
|
boolean isCreated = directory.mkdirs(); |
|
|
|
@@ -36,11 +53,12 @@ public class YamlUtils { |
|
|
|
} |
|
|
|
|
|
|
|
String fullPath = path + "/" + fileName + ".yaml"; |
|
|
|
|
|
|
|
try (FileWriter writer = new FileWriter(fullPath)) { |
|
|
|
yaml.dump(data, writer); |
|
|
|
} catch (IOException e) { |
|
|
|
File dumpFile = new File(fullPath); |
|
|
|
try { |
|
|
|
Yaml.dump(data, dumpFile); |
|
|
|
} catch (FileNotFoundException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
@@ -53,7 +71,8 @@ public class YamlUtils { |
|
|
|
public static Map<String, Object> loadYamlFile(String filePath) { |
|
|
|
Yaml yaml = new Yaml(); |
|
|
|
try (InputStream inputStream = new FileInputStream(new File(filePath))) { |
|
|
|
return yaml.load(inputStream); |
|
|
|
Map<String, Object> load = (Map<String, Object>) yaml.load(inputStream); |
|
|
|
return load; |
|
|
|
} catch (IOException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
return null; |
|
|
|
|