web123456

Java reads and writes YAML files

  • import ;
  • import .*;
  • import ;
  • public class Main {
  • public static void main(String[] args) {
  • try {
  • Map m1,m2,m3,m4;
  • FileWriter fw;
  •     /* Read */
  • Yaml y = new Yaml();
  •     //Create file object
  • File file = new File("");
  •     //Parse yaml content into map table
  • m1 = (Map) (new FileInputStream(file));
  •     //Get the "details" key in the first-level key as an object to further obtain the key and value of the lower level
  • m2 = (Map) ("details");
  • m3 = (Map) ("friends");
  •             //The email key belongs to the third level, assign its key to its value
  • ("email", "asdasd@");
  •     //Get the "info" key in the third level key
  • m4 = (Map) ("info");
  •     // Assign the fourth level key "tel" to 2222
  • ("tcp", 2222);
  •     /* Write */
  •     //Initialize the filewriter object for write operations
  • fw = new FileWriter(file);
  •     // Use snakeyaml dump method to parse the map class into yaml content
  • ((m1));
  •     //Write to file
  • ();
  • ();
  • } catch (IOException e) {
  • ();
  • } catch (Exception e) {
  • ();
  • }
  • }
  • }