在Java中加载YAML [英] load YAML in Java
本文介绍了在Java中加载YAML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用以下代码在Java中读取YAML文件:
I am trying to read a YAML file in Java using the following code:
public class LoadFile {
public static void main(String[] args) throws IOException {
Yaml yaml = new Yaml();
InputStream inputStream = LoadFile.class
.getClassLoader()
.getResourceAsStream("ABSOLUTE PATH TO YAML");
Object obj = yaml.load(inputStream);
System.out.println(obj);
}
}
但是我得到了这个异常,我不明白为什么会发生.
But I get this exception which I don't understand why it occurs.
线程"main"中的异常org.yaml.snakeyaml.error.YAMLException:java.io.IOException:流已关闭
Exception in thread "main" org.yaml.snakeyaml.error.YAMLException: java.io.IOException: Stream closed
推荐答案
导致问题的原因尚不明显,但需要检查一些内容: 1-确保路径正确 2-确保文件格式正确
It's not obvious what causes the problem but there is some stuff to check: 1- make sure that the path is correct 2- make sure that the file format is correct
旁注:
您可以将Object更改为Map<String,Object>
以便进行更好的投射
side note:
you can change Object to be a Map<String,Object>
for better casting
这篇关于在Java中加载YAML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文