我希望加载属性文件和命令行参数,然后在运行时动态配置日志记录,我以前可以这样做:
Properties configuration;
...
ByteArrayOutputStream os = new ByteArrayOutputStream();
ByteArrayInputStream is;
byte[] buf;
try {
configuration.store(os, "logging");
buf = os.toByteArray();
is = new ByteArrayInputStream(buf);
java.util.logging.LogManager.getLogManager().readConfiguration(is);
} catch (IOException e) {
System.err.println("Failed to configure java.util.logging.LogManager");
}属性很好,但是可以用PropertiesConfiguration完成吗?
(FYI曾希望利用configuration提供的属性数组)
发布于 2013-10-29 09:45:02
使用ConfigurationConverter将PropertiesConfiguration转换为标准的poperties文件
发布于 2013-10-23 05:12:00
不是的。但您可以将PropertiesConfiguration转换为属性
public static Properties configurationAsProperties(){
Properties fromConfiguration = new Properties();
Iterator<String> keys = configuration.getKeys();
while (keys.hasNext()) {
String key = keys.next();
String value = asString(configuration.getProperty(key));
fromConfiguration.setProperty(key,value);
// System.out.println(key + " = " + value);
}
return fromConfiguration;
}只是不要丢失那些逗号分隔的值(configuration.getString只会返回第一个)
private static String asString(Object value) {
if (value instanceof List) {
List<?> list = (List<?>) value;
value = StringUtils.join(list.iterator(), ",");
}
return (String) value;
}https://stackoverflow.com/questions/19531600
复制相似问题