英文:
SnakeYaml keep IOException: Stream closed
问题
代码部分已翻译如下:
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);
}
英文:
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);
}
答案1
得分: 0
我找到了答案;
因为yaml文件与我的类在同一个包中,我应该去掉.getClassLoader
。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论