如何使用SnakeYaml写入YAML文件? [英] How do I write to a YAML file using SnakeYaml?

查看:425
本文介绍了如何使用SnakeYaml写入YAML文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下代码:

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
}

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

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.

现在按照 SnakeYaml的文档,创建我只需要的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.

但是没有地方(至少我看不到)描述了如何在特定路径下创建Yaml文件!

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

我发现的唯一东西是:

"Yaml.dump(对象数据)方法接受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);
}

"Yaml.dump(对象数据,Writer输出)会将生成的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.

推荐答案

如果我已经理解了这个问题,那么它似乎与YAML或SnakeYAML本身没有任何关系,而是与您编写的方式有关Java中的特定文件.基本上,您复制的第二个示例显示的是如何将对象转储到任意 java.io.Writer 对象(尽管它们使用 StringWriter ,因为这不会向磁盘写入任何内容.如果要修改此示例以写入特定文件,可以通过使用

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.Writer object (though they use a StringWriter as 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:

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

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

Which will dump the map data to a YAML file. Note that it is not normally necessary to create the file yourself before opening the FileWriter as the FileWriter will handle that for you.

这篇关于如何使用SnakeYaml写入YAML文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆