一切都很好,直到我将文件移动到相对于资源文件夹的内部文件夹 /src/main/resources/settings
Files.newDirectoryStream(Paths.get("./src/main/resources/settings"), path -> path.toFile().isFile())
.forEach(value -> {
if (value.toString().endsWith("properties")) {
Properties currentProp = new Properties();
try {
currentProp.load(this.getClass().getClassLoader().getResourceAsStream(value.toFile().getName()));
} catch (IOException e) {
e.printStackTrace();
}
}
});
getResourceAsStream(value.toFile().getName())
- 返回 null
如上所述,如果文件直接位于资源文件夹中,则一切运行顺利。
最佳答案
我相信getResourceAsStream
会找到与类路径相关的文件。假设您的文件夹结构是标准的 Maven 结构,您只需将 settings
传递给 Path.get
。使用您现在拥有的代码,它将在 src/main/resources/src/main/resources/setting 中查找,这可能不是您想要的。
关于java - 路径更改时 getResourceAsStream 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57176763/