SnakeYaml保留IOException:流已关闭

huangapple go评论78阅读模式
英文:

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(&quot;config.yml&quot;);
		Map&lt;String, Object&gt; obj = yaml.load(inputStream);
		System.out.println(obj);
		
}

SnakeYaml保留IOException:流已关闭

答案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.

huangapple
  • 本文由 发表于 2020年9月3日 00:09:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63709403.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定