Java 如何使用 SnakeYaml 写入 YAML 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24949505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 15:55:26  来源:igfitidea点击:

How do I write to a YAML file using SnakeYaml?

javafileoutputfileoutputstreamsnakeyaml

提问by Jonas Bartkowski

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 aiming to write the given Object O at the given key, into the YAML file at the given path. (if it doesn't exist it is created.) But obviously the main part is still missing.

此方法旨在将给定键的给定对象 O 写入给定路径的 YAML 文件中。(如果它不存在,则创建它。)但显然主要部分仍然缺失。

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

现在按照SnakeYaml 的文档,要创建 YAML,我只需要创建一个映射并将对象放入正确的键中,我做到了。

But nowhere (at least I don't see it) is described how to create a yaml file at a certain path!

但是没有任何地方(至少我没有看到)描述了如何在某个路径上创建 yaml 文件!

The only thing I found was:

我发现的唯一一件事是:

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

“Yaml.dump(Object data) 方法接受一个 Java 对象并生成一个 YAML 文档”

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 produced YAML document into the specified file/stream."

“Yaml.dump(Object data, Writer output) 将生成的 YAML 文档写入指定的文件/流。”

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());
}

But still, even though it says exactly that at the second bit of code, it doesn't seem to support the manipulation of a certain file and it's certainly not shown how to do it.

但是,尽管它在代码的第二位准确说明了这一点,但它似乎不支持对某个文件的操作,而且肯定没有显示如何操作。

Is it only me or does the documentation feel very cryptic and specified? Half of it is about special applications that I have never even heard about. I feel really dumb just by looking at it and it makes me kind of angry.

是只有我一个人还是文档感觉非常神秘和具体?其中一半是关于我从未听说过的特殊应用程序。光是看着就觉得自己很蠢,这让我有点生气。

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

无论如何; 我真的很感激你能给我的任何帮助。

采纳答案by ig0774

If I've understood the question, it doesn't seem to have anything to do with YAML or SnakeYAML per se, but to do with how you write to a specific file in Java. Basically, what the second example you copied is showing is how to dump an object to an arbitrary java.io.Writerobject (though they use a StringWriteras this will not write anything to disk). If you want to modify this example to write to a particular file, you can do so by making use of a FileWriter, like so:

如果我理解了这个问题,它本身似乎与 YAML 或 SnakeYAML 没有任何关系,而是与您如何在 Java 中写入特定文件有关。基本上,您复制的第二个示例显示的是如何将对象转储到任意java.io.Writer对象(尽管它们使用 aStringWriter因为这不会将任何内容写入磁盘)。如果要修改此示例以写入特定文件,可以使用 a 来实现FileWriter,如下所示:

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 dump the map datato a YAML file. Note that it is not normally necessary to create the file yourself before opening the FileWriteras the FileWriterwill handle that for you.

这会将地图转储data到 YAML 文件。请注意,通常不需要在打开文件之前自己创建文件,FileWriter因为它FileWriter会为您处理。

回答by Aliaksandr Kavalenka

Else one way to write data

另一种写入数据的方式

 Map<String, String> map = new HashMap<>();
        map.put("key1", "value1");
        map.put("key2", "value2");
        map.put("key3", "value3");

        YamlWriter writer = new YamlWriter(new FileWriter("test.yml"));
        writer.write(map);
        writer.close();

Output:

输出:

  key1: value1
  key2: value2
  key3: value3

For me, in this case, I see a more readable output. From the first solution, we will see output like this:

对我来说,在这种情况下,我看到了更具可读性的输出。从第一个解决方案中,我们将看到如下输出:

{key1: value1, key2: value2, key3: value3}

If we will have a lot of data it will be hard to read

如果我们将有大量数据,将很难阅读

P.S. we need some dependency

PS我们需要一些依赖

    <dependency>
        <groupId>com.esotericsoftware.yamlbeans</groupId>
        <artifactId>yamlbeans</artifactId>
        <version>1.13</version>
    </dependency>