获取“java.io.IOException:流已关闭”错误,当尝试从相对路径读取文件时。

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

Getting "java.io.IOException: Stream closed", when trying to read file from relative path

问题

我正在尝试读取 YAML 文件。起初,我有绝对路径,一切正常。现在,当我有一个要部署到服务器上的 WAR 文件时,我想将绝对路径更改为相对路径。从那时起,它就报错了。

代码:

Yaml yaml = new Yaml();
InputStream inputStream = getClass().getResourceAsStream("src/main/resources/config/jobnames.yml");
HashMap yamlMap = yaml.load(inputStream);

异常堆栈:

org.yaml.snakeyaml.error.YAMLException: java.io.IOException: Stream closed
	org.yaml.snakeyaml.reader.StreamReader.update(StreamReader.java:218)
	org.yaml.snakeyaml.reader.StreamReader.ensureEnoughData(StreamReader.java:176)
	org.yaml.snakeyaml.reader.StreamReader.ensureEnoughData(StreamReader.java:171)
	org.yaml.snakeyaml.reader.StreamReader.peek(StreamReader.java:126)
	org.yaml.snakeyaml.scanner.ScannerImpl.scanToNextToken(ScannerImpl.java:1177)
	org.yaml.snakeyaml.scanner.ScannerImpl.fetchMoreTokens(ScannerImpl.java:287)
	org.yaml.snakeyaml.scanner.ScannerImpl.checkToken(ScannerImpl.java:227)
	org.yaml.snakeyaml.parser.ParserImpl$ParseImplicitDocumentStart.produce(ParserImpl.java:195)
	org.yaml.snakeyaml.parser.ParserImpl.peekEvent(ParserImpl.java:158)
	org.yaml.snakeyaml.parser.ParserImpl.checkEvent(ParserImpl.java:148)
	org.yaml.snakeyaml.composer.Composer.getSingleNode(Composer.java:107)
	org.yaml.snakeyaml.constructor.BaseConstructor.getSingleData(BaseConstructor.java:141)
	org.yaml.snakeyaml.Yaml.loadFromReader(Yaml.java:525)
	org.yaml.snakeyaml.Yaml.load(Yaml.java:453)

请帮我找出问题在哪里?谢谢。

英文:

I am trying to read yaml file. At first i had absolute path,it was working fine. Now when i have war file to be deployed in server,i want to change the absolute to relative path. From then it is giving the error.
Code:

    Yaml yaml = new Yaml();
    InputStream inputStream = getClass().getResourceAsStream("src/main/resources/config/jobnames.yml");
    HashMap yamlMap = yaml.load(inputStream);

stack of exception:

org.yaml.snakeyaml.error.YAMLException: java.io.IOException: Stream closed
org.yaml.snakeyaml.reader.StreamReader.update(StreamReader.java:218)
org.yaml.snakeyaml.reader.StreamReader.ensureEnoughData(StreamReader.java:176)
org.yaml.snakeyaml.reader.StreamReader.ensureEnoughData(StreamReader.java:171)
org.yaml.snakeyaml.reader.StreamReader.peek(StreamReader.java:126)
org.yaml.snakeyaml.scanner.ScannerImpl.scanToNextToken(ScannerImpl.java:1177)
org.yaml.snakeyaml.scanner.ScannerImpl.fetchMoreTokens(ScannerImpl.java:287)
org.yaml.snakeyaml.scanner.ScannerImpl.checkToken(ScannerImpl.java:227)
org.yaml.snakeyaml.parser.ParserImpl$ParseImplicitDocumentStart.produce(ParserImpl.java:195)
org.yaml.snakeyaml.parser.ParserImpl.peekEvent(ParserImpl.java:158)
org.yaml.snakeyaml.parser.ParserImpl.checkEvent(ParserImpl.java:148)
org.yaml.snakeyaml.composer.Composer.getSingleNode(Composer.java:107)
org.yaml.snakeyaml.constructor.BaseConstructor.getSingleData(BaseConstructor.java:141)
org.yaml.snakeyaml.Yaml.loadFromReader(Yaml.java:525)
org.yaml.snakeyaml.Yaml.load(Yaml.java:453)

please help me out what is the issue? Thanks

答案1

得分: 3

因为您正在使用 ClassLoader,所以路径应该是相对路径而不是绝对路径,因为 resources 直接在类路径上可用(同时在您的代码中缺少获取 ClassLoader 的部分)。

您可以尝试使用下面的代码来替代使用绝对路径:

Yaml yaml = new Yaml();
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("config/jobnames.yml");
HashMap yamlMap = yaml.load(inputStream);

这样应该可以解决问题!

英文:

Since you are using a ClassLoader, the path should be relative and not absolute, as resources is directly available on the classpath (Also getting ClassLoader is missing in your code)

Can you try this instead of using absolute path-

Yaml yaml = new Yaml();
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("config/jobnames.yml");
HashMap yamlMap = yaml.load(inputStream);

That should do the trick!

答案2

得分: 1

ClassLoader.getResourceAsStream()从类路径加载资源,如此处所述: https://docs.oracle.com/javase/8/docs/api/java/lang/ClassLoader.html#getResource-java.lang.String-

因此,您必须省略“src/main/resources”,因为这只是您的项目设置路径。"/config/jobnames.yml"应该可以工作。

英文:

ClassLoader.getResourceAsStream() loads resources from the classpath as described here: https://docs.oracle.com/javase/8/docs/api/java/lang/ClassLoader.html#getResource-java.lang.String-

So you have to omit "src/main/resources" since this just happens to be your project setup path. "/config/jobnames.yml" should work.

huangapple
  • 本文由 发表于 2020年8月29日 16:19:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63644891.html
匿名

发表评论

匿名网友

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

确定