-
-
Notifications
You must be signed in to change notification settings - Fork 53
YAML Printer
Mihai edited this page Apr 12, 2020
·
5 revisions
We offer a YamlPrinter
which you can create and use like this:
final YamlMapping map = ...;
final YamlPrinter printer = Yaml.createYamlPrinter(/* any java.io.Writer */);
printer.print(map);
It works with any implementation of java.io.Writer
. For instance, to print the YAML to a file, all you have to do is:
final YamlMapping map = ...;
final YamlPrinter printer = Yaml.createYamlPrinter(
new FileWriter("/path/to/map.yml")
);
printer.print(map); //file map.yml will be created and written.
All the toString()
methods are already implemented using a YamlPrinter
that works with a StringWriter
.
This means, the following code snippets are equivalent:
final YamlMapping map = ...;
System.out.println(map.toString());
final YamlMapping map = ...;
final StringWriter stgw = new StringWriter();
final YamlPrinter printer = Yaml.createYamlPrinter(stgw);
printer.print(map);
System.out.println(stgw.toString());
Pay attention, the provided java.io.Writer
will be flushed and closed at the end of the printing, which means you can only call the print(...)
method once. The second call should throw an IOException
.