プロパティファイル

  /**
   * プロパティファイルから値を取得する。
   *
@param key キー
   *
@return
   *
@throws IOException
   *
@throws Exception
   */
 
public String getProperty(final String key) throws IOException {
   
String propFile = "[プロパティファイル名]";//実ファイルのパス
   
final InputStream input = this.getClass().getResourceAsStream(propFile);

   
final Properties prop = new Properties();
    prop.load
(input);
    input.close
();

   
return prop.getProperty(key);
 
}
 
 /**
   * プロパティファイルから値を取得する。(リソースバンドル使用)
   *
@param key キー
   *
@return
   */
 
public String getProperty(final String key) {
   
String propFile = "[プロパティファイル名]";//javaのクラスと同様に完全限定名(例 : org.dyndns.rhmusic.resourceパッケージにある[message.properties]の場合"org.dyndns.rhmusic.resource.message")
   
final ResourceBundle bundle = ResourceBundle.getBundle(propFile);

   
return bundle.getString(key);
 
}

Java TipsのTOPに戻る