load YAML in Java

断了今生、忘了曾经 提交于 2020-06-28 13:09:22

问题


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.

Exception in thread "main" org.yaml.snakeyaml.error.YAMLException: java.io.IOException: Stream closed


回答1:


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

side note: you can change Object to be a Map<String,Object> for better casting



来源:https://stackoverflow.com/questions/62031850/load-yaml-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!