How to write a YAML file using SnakeYaml?

Consider the following code:

public static void dumpObjectToYaml(String key, Object O, String path) throws IOException { Map<String, Object> data = new HashMap<>(); data.put(key, O); File F = new File(path); F.mkdirs(); F.createNewFile(); //write data to File } 

This method is designed to write this O to a given key in a YAML file at a given path. (if it does not exist, it is created.) But, obviously, the main part is still missing.

Now, following the SnakeYaml documentation to create YAML, I need to create a map and put the objects in the right keys, which I did.

But nowhere (at least I don’t see it) describes how to create a yaml file in a specific path!

The only thing I found is:

"The Yaml.dump (Object data) method accepts a Java object and creates a YAML document

 public void testDump() { Map<String, Object> data = new HashMap<String, Object>(); data.put("name", "Silenthand Olleander"); data.put("race", "Human"); data.put("traits", new String[] { "ONE_HAND", "ONE_EYE" }); Yaml yaml = new Yaml(); String output = yaml.dump(data); System.out.println(output); } 

and

"Yaml.dump (object data, Writer output) will write the created YAML document to the specified file / stream."

 public void testDumpWriter() { Map<String, Object> data = new HashMap<String, Object>(); data.put("name", "Silenthand Olleander"); data.put("race", "Human"); data.put("traits", new String[] { "ONE_HAND", "ONE_EYE" }); Yaml yaml = new Yaml(); StringWriter writer = new StringWriter(); yaml.dump(data, writer); System.out.println(writer.toString()); } 

Nevertheless, although he definitely says that on the second bit of code he does not seem to support manipulating a certain file, and, of course, it is not shown how to do this.

Is this just me or does the documentation look very mysterious and refined? Half of them relate to special applications that I have not even heard of. I feel very stupid just looking at him and it makes me angry.

Anyway; I would really appreciate any help you could give me.

+6
source share
1 answer

If I understood this question, it seems to have nothing to do with YAML or SnakeYAML as such, but with the way you write to a specific file in Java. Basically, it is shown, as shown by the second example that you copied: how to dump an object to an arbitrary java.io.Writer object (although they use StringWriter , since this will not write anything to disk). If you want to modify this example to write to a specific file, you can do this using FileWriter , for example:

 public void testDumpWriter() { Map<String, Object> data = new HashMap<String, Object>(); data.put("name", "Silenthand Olleander"); data.put("race", "Human"); data.put("traits", new String[] { "ONE_HAND", "ONE_EYE" }); Yaml yaml = new Yaml(); FileWriter writer = new FileWriter("/path/to/file.yaml"); yaml.dump(data, writer); } 

Which will output the data card to a YAML file. Please note: there is usually no need to create a file before opening FileWriter , as FileWriter will handle this for you.

+10
source

Source: https://habr.com/ru/post/972817/


All Articles