laodメソッドを個別に呼び出さずにJavaでプロパティファイルをロードする方法プロパティオブジェクト自体のインスタンス化中にファイルをロードしたい。下に貼り付けたようですが、うまくいきません。
class test{
Properties configFile = new Properties(load(new FileInputStream("config.properties"));
}
    laodメソッドを個別に呼び出さずにJavaでプロパティファイルをロードする方法プロパティオブジェクト自体のインスタンス化中にファイルをロードしたい。下に貼り付けたようですが、うまくいきません。
class test{
Properties configFile = new Properties(load(new FileInputStream("config.properties"));
}
    これを行うための別のメソッドを作成するだけです。他の場所で使用できるヘルパークラスである可能性があります。
public class PropertiesHelper {
    public static Properties loadFromFile(String file) throws IOException {
        Properties properties = new Properties();
        FileInputStream stream = new FileInputStream(file);
        try {
            properties.load(stream);
        } finally {
            stream.close();
        }
        return properties;
    }
}
の可能性があるためIOException、これをどこから呼び出すかについては注意が必要です。インスタンス初期化子で使用する場合は、すべてのコンストラクターがをスローできることを宣言する必要がありますIOException。
これに沿った何か:
class Test {
    Properties configFile = new Properties() {{ load(new FileInputStream("config.properties")); }};
}
ここでは実際にプロパティをサブクラス化し、その初期化セクションを使用しています。load(..)は例外をスローする可能性があるため、追加する必要がありますtry { ... } catch () {}