0
public static Properties defaultProps = new Properties();
static {
try {
FileInputStream in = new FileInputStream("config.properties");
defaultProps.load(in);
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static String getProperty(String database) {
return defaultProps.getProperty(database);
}
public static void main(String[] args) {
System.out.println(...key database?);
// this is the part where I try to test if I can print the property 'database'
// I also try to make it available to other classes, tried using public statics,
}
これはプロパティファイルconfig.properties
からプロパティを取得する私のコードです。しかし、プロパティN(ここではdatabase
)を印刷し、他のクラスでプロパティNを使用できるようにしたい。クラス外のgetProperty()へのアクセス
ご協力ありがとうございます。
を呼び出し、問題は何ですか? 'getProperty(文字列データベース)では、' database'はキーパラメータの名前であり、値ではありません。おそらく 'YourClass.getProperty(" database ")'のように呼びたいと思うかもしれませんが、 '' YourClass.getProperty( "その他の完全に独立したプロパティ")のような呼び出しも同様に動作します: 'String database'は_parameter_が_value_が '' database ''ではないことに注意してください。 – Thomas
静的なイニシャライザの内部でIO(ファイルの読み込みを含む)を実行するのはかなり悪いことです。つまり、あなたの問題は何ですか? Properties変数はpublicで、読み込むだけです。 – markspace
Brainfart。申し訳ありませんが、お返事ありがとうございます! – Chuchoter