gpt4 book ai didi

java - 无法读取 IntelliJ 中的属性文件

转载 作者:行者123 更新时间:2023-12-01 22:56:11 27 4
gpt4 key购买 nike

我的文件夹结构是:

Main
|
.idea
resources
src
|
main
|
java
|
com.read.properties
|
config.properties
a.java

我在a.java中的代码是:

Properties prop = new Properties();
InputStream input = null;

try {

String filename = "config.properties";
input = getClass().getClassLoader().getResourceAsStream(filename);
if (input == null) {
System.out.println("Sorry, unable to find " + filename);
return Response.status(400).build();
}

prop.load(input);

Enumeration<?> e = prop.propertyNames();
while (e.hasMoreElements()) {
String key = (String) e.nextElement();
String value = prop.getProperty(key);
System.out.println("Key : " + key + ", Value : " + value);
}

} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (input != null) {
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
}

我无法读取属性文件,我不断收到 IO 错误。我尝试更改默认类路径

来自运行->编辑配置

当我在 eclipse 中尝试类似的操作时,我可以很容易地读取该文件,这让我相信我在 intelliJ 中的某些配置是错误的。如果有人能指出我正确的方向,我将非常感激

最佳答案

我对此不确定,但在我的 IDEA (Maven) 项目中,我将资源保存在 src/main|test/resources 下,我可以很好地阅读它们:

src
main
resources
path
file


getClass().getClassLoader().getResourceAsStream("path/file");

您需要指定文件的“完整”路径。

关于java - 无法读取 IntelliJ 中的属性文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24023931/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com