我想,好像是apons的commons-configuration可以/支持从属性文件获取属性作为地图
到目前为止,我已设法使用以下代码段间接执行此操作
Map<String, T> map = new LinkedHashMap<>();
Configuration subset = config.subset(key);
if (!subset.isEmpty()) {
Iterator it = subset.getKeys();
while (it.hasNext()) {
String k = (String) it.next();
//noinspection unchecked
T v = (T) subset.getProperty(k);
map.put(k, v);
}
}
return map;
有谁知道比这更直接的方式?
非常感谢
答案 0 :(得分:4)
我更喜欢你这样做,但如果你愿意:ConfigurationMap
Map<Object,Object> config = new ConfigurationMap(subset);
答案 1 :(得分:0)
使用apache commons confuguration2
获取所有属性作为MapParameters params = new Parameters();
File propertiesFile = new File("properties.properties");
FileBasedConfigurationBuilder<FileBasedConfiguration> builder =
new FileBasedConfigurationBuilder<FileBasedConfiguration>(PropertiesConfiguration.class)
.configure(params.fileBased()
.setFile(propertiesFile)
.setEncoding("UTF-8"));
Configuration config = builder.getConfiguration();
Map<Object,Object> cfg = new ConfigurationMap(config);
cfg.entrySet();
退房:
for (Map.Entry entry : cfg.entrySet()) {
System.out.println(entry.getKey() + ", " + entry.getValue());
}