我正在尝试创建一个实用程序类 ReadPropertyUtil.java
用于从属性文件中读取数据。虽然我的课程位于 util 目录下,但我的 skyscrapper.properties
文件放在其他目录中。
但是,当我尝试使用 [ResourceBundle][1]
访问属性时,出现异常,无法加载该捆绑包。
下面是关于我如何读取属性的代码以及显示我的目录结构的图像。
ReadPropertiesUtil.java
/**
* Properties file name.
*/
private static final String FILENAME = "skyscrapper";
/**
* Resource bundle.
*/
private static ResourceBundle resourceBundle = ResourceBundle.getBundle(FILENAME);
/**
* Method to read the property value.
*
* @param key
* @return
*/
public static String getProperty(final String key) {
String str = null;
if (resourceBundle != null) {
str = resourceBundle.getString(key);
LOGGER.debug("Value found: " + str + " for key: " + key);
} else {
LOGGER.debug("Properties file was not loaded correctly!!");
}
return str;
}
目录结构
此行给出错误 private static ResourceBundle resourceBundle = ResourceBundle.getBundle(FILENAME);
我无法理解为什么这不起作用以及解决方案是什么。 src
文件夹已完全添加到构建路径中。
原文由 roger_that 发布,翻译遵循 CC BY-SA 4.0 许可协议
尝试使用资源的 完全限定名称: