问题
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