我正在使用Apache Commons Configuration
库和PropertiesConfiguration
。我的应用程序在启动后立即加载配置文件,如下所示:
public PropertiesConfiguration loadConfigFile(File configFile) throws ConfigurationNotFoundException {
try {
if (configFile != null && configFile.exists()) {
config.load(configFile);
config.setListDelimiter(';');
config.setAutoSave(true);
config.setReloadingStrategy(new FileChangedReloadingStrategy());
setConfigLoaded(true);
}
else {
throw new ConfigurationNotFoundException("Configuration file not found.");
}
} catch (ConfigurationException e) {
logger.warn(e.getMessage());
setDefaultConfigValues(config);
config.setFile(configFile);
}
return config;
}
我的问题是,如何验证configFile
,以确保该文件中没有缺少的属性,在我的代码中访问属性时不会出现NullPointerException
,例如:
PropertiesConfiguration config = loadConfig(configFile);
String rootDir = config.getString("paths.download"); // I want to be sure that this property exists right at the app start
我在文档或谷歌上没有找到任何内容,只是有一些关于XML验证的东西。
目标是在程序启动时向用户提供反馈,说明配置文件已经损坏。
是否没有内置机制来处理属性文件?