import org.yaml.snakeyaml.Yaml; import java.io.FileInputStream; import java.io.FileNotFoundException; public class SnakeYAMLExample { public static void main(String[] args) { try { Yaml yaml = new Yaml(); FileInputStream inputStream = new FileInputStream("example.yaml"); MyConfig config = yaml.loadAs(inputStream, MyConfig.class); System.out.println(config.getName()); System.out.println(config.getAge()); } catch (FileNotFoundException e) { e.printStackTrace(); } } } public class MyConfig { private String name; private int age; } import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.constructor.Constructor; import org.yaml.snakeyaml.constructor.ConstructorException; import org.yaml.snakeyaml.representer.Representer; import org.yaml.snakeyaml.introspector.BeanAccess; public class SnakeYAMLConfig { public static void main(String[] args) { Yaml yaml = new Yaml(new Constructor(MyConfig.class), new Representer(), new DumperOptions()); yaml.setBeanAccess(BeanAccess.FIELD); try { FileInputStream inputStream = new FileInputStream("example.yaml"); MyConfig config = yaml.loadAs(inputStream, MyConfig.class); System.out.println(config.getName()); System.out.println(config.getAge()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (ConstructorException e) { e.printStackTrace(); } } }


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