0

プロパティ ファイルとコマンド ライン引数を読み込み、実行時にログを動的に構成したいのですが、以前は次のように実行できました。

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");
}

Properties には最適ですが、PropertiesConfiguration で実行できますか?

(参考までに、commons-configurationが提供するプロパティの配列を利用したいと思っていました)

4

2 に答える 2

1

ConfigurationConverterを使用して、PropertiesConfiguration を標準の poperties ファイルに変換します

于 2013-10-29T09:45:02.963 に答える
0

いいえ。ただし、 PropertiesConfiguration を Properties に変換できます

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;
}
于 2013-10-23T05:12:00.990 に答える