2017-04-06 20 views
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()へのアクセス

ご協力ありがとうございます。

+0

を呼び出し、問題は何ですか? 'getProperty(文字列データベース)では、' database'はキーパラメータの名前であり、値ではありません。おそらく 'YourClass.getProperty(" database ")'のように呼びたいと思うかもしれませんが、 '' YourClass.getProperty( "その他の完全に独立したプロパティ")のような呼び出しも同様に動作します: 'String database'は_parameter_が_value_が '' database ''ではないことに注意してください。 – Thomas

+0

静的なイニシャライザの内部でIO(ファイルの読み込みを含む)を実行するのはかなり悪いことです。つまり、あなたの問題は何ですか? Properties変数はpublicで、読み込むだけです。 – markspace

+0

Brainfart。申し訳ありませんが、お返事ありがとうございます! – Chuchoter

答えて

0

ので、ちょうど

System.out.println(getProperty("database")); 
+0

ありがとうございました。これはうまくいきました。 – Chuchoter

関連する問題