import net.sourceforge.yamlbeans.YamlReader; import net.sourceforge.yamlbeans.YamlWriter; import java.io.FileReader; import java.io.FileWriter; public class YamlBeansExample { public static void main(String[] args) { Book book = new Book("Harry Potter", "J.K. Rowling", 2000); try { YamlWriter writer = new YamlWriter(new FileWriter("book.yaml")); writer.write(book); writer.close(); } catch (Exception e) { e.printStackTrace(); } try { YamlReader reader = new YamlReader(new FileReader("book.yaml")); Book restoredBook = reader.read(Book.class); reader.close(); System.out.println(restoredBook); } catch (Exception e) { e.printStackTrace(); } } } class Book { private String title; private String author; private int releaseYear; public Book() {} public Book(String title, String author, int releaseYear) { this.title = title; this.author = author; this.releaseYear = releaseYear; } }


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