Search code examples
javayamlsnakeyaml

SnakeYaml keep IOException: Stream closed


As I tried to run the code from this webiste, I keep getting the same error no matter what I do to try and resolve it. I've tried:

  • Checking the name is correct and exists (dunno if it is a path problem, I'm directly referencing the file ("example.txt") in the input stream and the file is in the same package as the java file.
  • Updating the libraries
  • Putting the file name directly in yaml.load which just throws another exception (class java.lang.String cannot be cast to class java.util.Map (java.lang.String and java.util.Map are in module java.base of loader 'bootstrap'))
  • Creating a new java project, same error.
  • Find a solution on the internet.
private void editConfig(String id) {
        
        Yaml yaml = new Yaml();
        InputStream inputStream = this.getClass()
          .getClassLoader()
          .getResourceAsStream("config.yml");
        Map<String, Object> obj = yaml.load(inputStream);
        System.out.println(obj);
        
}

Here is my file structure


Solution

  • I found the answer;

    Because the yaml file is in the same package as my class, I am supposed to get rid of the .getClassLoader.