访问 .yml 文件时发生流关闭错误

问题描述 投票:0回答:1

无法访问

.yml
文件,因为未读取该文件。不确定路径是否错误,或者考虑到我使用的是 Mac,我需要不同的路径设置方式。

我正在尝试访问位于

.yml
下的
src/yml/server.yml
并收到 Stream Closed 错误。

测试类位于此处供参考:

src/smokeTest/java/package.package.../SmokeTest.java

代码:

Yaml yaml = new Yaml();
InputStream ymlReader = this.getClass().getClassLoader().getResourceAsStream("yml/server.yml");

Map<String, Object> yamlMaps = yaml.load(ymlReader);

LOG.info("Value of Status Flag", yamlMaps.get("status"));

错误:

org.yaml.snakeyaml.error.YAMLException: java.io.IOException: Stream closed
    at app//org.yaml.snakeyaml.reader.StreamReader.update(StreamReader.java:218)
    at app//org.yaml.snakeyaml.reader.StreamReader.ensureEnoughData(StreamReader.java:176)
    at app//org.yaml.snakeyaml.reader.StreamReader.ensureEnoughData(StreamReader.java:171)
    at app//org.yaml.snakeyaml.reader.StreamReader.peek(StreamReader.java:126)
    at app//org.yaml.snakeyaml.scanner.ScannerImpl.scanToNextToken(ScannerImpl.java:1223)
    at app//org.yaml.snakeyaml.scanner.ScannerImpl.fetchMoreTokens(ScannerImpl.java:328)
    at app//org.yaml.snakeyaml.scanner.ScannerImpl.checkToken(ScannerImpl.java:250)
    at app//org.yaml.snakeyaml.parser.ParserImpl$ParseImplicitDocumentStart.produce(ParserImpl.java:214)
    at app//org.yaml.snakeyaml.parser.ParserImpl.peekEvent(ParserImpl.java:166)
    at app//org.yaml.snakeyaml.parser.ParserImpl.checkEvent(ParserImpl.java:156)
    at app//org.yaml.snakeyaml.composer.Composer.getSingleNode(Composer.java:145)
    at app//org.yaml.snakeyaml.constructor.BaseConstructor.getSingleData(BaseConstructor.java:157)
    at app//org.yaml.snakeyaml.Yaml.loadFromReader(Yaml.java:491)
    at app//org.yaml.snakeyaml.Yaml.load(Yaml.java:429)

我尝试了这些解决方案,但它们不起作用:

尝试从相对路径读取文件时出现“java.io.IOException:流已关闭”

SnakeYaml 保持 IOException:流已关闭

java yaml inputstream snakeyaml
1个回答
0
投票
this.getClass().getClassLoader().getResourceAsStream("yml/server.yml");

这只会获取当前类路径下的文件。您应该更改 yml 的路径以确保它位于当前的类加载器目录中

© www.soinside.com 2019 - 2024. All rights reserved.