0

commons-configuration v1.10 を使用しており、このクラスを使用PropertiesConfigurationしてアプリケーションのプロパティを読み込んでいます。カンマを含むプロパティがありますが、それを読み込むと区切られてしまい、カンマをなくす方法がわかりません。

プロパティをすべて順番にコンマを含めて吐き出しますが、問題になっているのは、「[」と「]」で囲まれているためです。

AbstractConfigurationには区切りを無効にする関数 がsetDelimiterParsingDisabled()ありますが、プロパティ ファイルを読み取るためにそれを実装したクラスが見つかりませんでした。

private static String readProperty(String property) {
    try {
        Configuration configuration = new PropertiesConfiguration(propertiesFile);
        return configuration.getProperty(property).toString();
    }
    catch(ConfigurationException e) {
        System.out.println("Issue reading " + property + " property");
        e.printStackTrace();
        System.exit(1);
        return "";
    }
}
4

4 に答える 4

0

コードを投稿すると役立ちます。

あなたが望む公式ドキュメントによるとAbstractConfiguration.setListDelimiter(null);

Stringメソッドを使用して、周囲の [] を見つけて削除することもできます。プロパティが という文字列にあると仮定しますprop

int start = prop.indexOf('[') + 1;
int end = prop.lastIndexOf(']');
String val = prop.substring(start,
    end > 0 ? end : prop.length());

indexOf文字が見つからない場合は -1 を返すため、区切り文字が存在しない場合でも、1 を追加して実際のプロパティ値の先頭を取得することは常に機能します。

于 2014-07-25T18:38:06.530 に答える
0
PropertiesConfiguration properties = new PropertiesConfiguration();
properties.setDelimiterParsingDisabled(true);

上記のコードは問題を解決します。java.util.Properties に切り替える必要はありません。階層は次のとおりです。

    PropertiesConfiguration
              |
              |(extends)
              |
    AbstractFileConfiguration
              |
              |(extends)
              |
    BaseConfiguration
              |
              |(extends)
              |
    AbstractConfiguration

私の場合、java.util.Properties ではサポートされていない変数置換をサポートしているため、特に Apache Properties Configuration を使用しました。

于 2016-08-18T06:51:12.370 に答える
0

PropertiesConfigurationApache Commons を使用したり、区切りなしでプロパティを取得したりできないようです。ただし、 にjava.util.Propertiesはこの問題がないため、 の代わりに を使用しましたPropertiesConfiguration

MKYong には、セットアップ方法の良い例があります。 http://www.mkyong.com/java/java-properties-file-examples/

于 2014-07-28T15:25:06.550 に答える