读取 returns 中的配置文件 null
Read config file in returns null
在我的程序中,我需要读入一个配置文件。不幸的是,每次读取它的尝试都没有正确获取数据。调试显示 inputReader 始终为空。配置文件在我的资源文件夹中。那样找不到还是为什么inputReader只有null?
private String result;
private final Properties property = new Properties();
public String getString(ConfigType type) {
InputStream inputReader = null;
try {
inputReader = getClass().getResourceAsStream("/resources/config");
if (inputReader != null) {
property.load(inputReader);
inputReader.close();
result = property.getProperty(type.getValue());
}
} catch (IOException exception){
}
}
据我所知,null
背后的原因是目录路径无效。
如果您没有资源文件夹的任何根目录或父目录,请不要在目录 "**/**resources/yourfile"
中使用 /
。
您不需要在 inputStream
中分配 null
。
InputStream inputStream = getClass().getResourceAsStream("resources/config.properties");
应该可以。
在我的程序中,我需要读入一个配置文件。不幸的是,每次读取它的尝试都没有正确获取数据。调试显示 inputReader 始终为空。配置文件在我的资源文件夹中。那样找不到还是为什么inputReader只有null?
private String result;
private final Properties property = new Properties();
public String getString(ConfigType type) {
InputStream inputReader = null;
try {
inputReader = getClass().getResourceAsStream("/resources/config");
if (inputReader != null) {
property.load(inputReader);
inputReader.close();
result = property.getProperty(type.getValue());
}
} catch (IOException exception){
}
}
据我所知,null
背后的原因是目录路径无效。
如果您没有资源文件夹的任何根目录或父目录,请不要在目录 "**/**resources/yourfile"
中使用 /
。
您不需要在 inputStream
中分配 null
。
InputStream inputStream = getClass().getResourceAsStream("resources/config.properties");
应该可以。