gpt4 book ai didi

java - 如何使用绝对路径读取配置文件?

转载 作者:行者123 更新时间:2023-11-29 06:33:44 24 4
gpt4 key购买 nike

我有一个配置文件,我必须从中读取一些属性。此配置文件存在于不同的位置,但具有相同的名称。我能够读取一个配置文件,但只能从我的项目中读取一个。我使用了以下代码:

Properties prop = new Properties();
String propFileName = "config.properties";


InputStream inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);

try {
prop.load(inputStream);
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
if (inputStream == null) {
try {
throw new FileNotFoundException("property file '" + propFileName + "' not found in the classpath");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

我必须使用完整路径打开配置文件,而不是 propFileName="config.properties"来插入配置文件的绝对路径。使用绝对路径无法打开配置文件。如何做到这一点?

最佳答案

InputStream inputStream = new FileInputStream("path");

将打开一个具有绝对路径的文件。

注意:这仅适用于文件,不适用于插件 jar 中包含的任何内容。

关于java - 如何使用绝对路径读取配置文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25724139/

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