import org.yaml.simple.Yaml; import java.io.FileReader; import java.util.Map; public class YAMLExample { public static void main(String[] args) { try { Yaml yaml = new Yaml(); FileReader reader = new FileReader("config.yaml"); Map<String, Object> data = yaml.load(reader); reader.close(); for (Map.Entry<String, Object> entry : data.entrySet()) { System.out.println(entry.getKey() + ": " + entry.getValue()); } } catch (Exception e) { e.printStackTrace(); } Student student = new Student("Tom", 18); String yamlString = new Yaml().dump(student); System.out.println(yamlString); String yamlData = "name: John age: 20"; Student parsedStudent = new Yaml().load(yamlData); System.out.println(parsedStudent.getName() + ", " + parsedStudent.getAge()); } } class Student { private String name; private int age; public Student(String name, int age) { this.name = name; this.age = age; } }


上一篇:
下一篇:
切换中文