「Java」ResourceBundle.getBundle()でプロパティファイルをstaticで読み込むサンプル

説明
ResourceBundle java.util.ResourceBundle.getBundle(String baseName)
パラメータ:
baseName – リソース・バンドルの基底名。完全指定クラス名
戻り値:
指定された基底名とデフォルトのロケールのリソース・バンドル

Javaコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.arkgame.study;
import java.util.ResourceBundle;
public class BundeDemo {
private static String user;
private static int cnt;
private static long price;
static {
ResourceBundle bundle = ResourceBundle.getBundle("common");
user = bundle.getString("user");
cnt = Integer.valueOf(bundle.getString("maxcount"));
price = Long.valueOf(bundle.getString("price"));
}
public static void main(String[] args) {
System.out.println("実行結果:");
System.out.println("username: " + user);
System.out.println("count: " + cnt);
System.out.println("password: " + price);
}
}
package com.arkgame.study; import java.util.ResourceBundle; public class BundeDemo { private static String user; private static int cnt; private static long price; static { ResourceBundle bundle = ResourceBundle.getBundle("common"); user = bundle.getString("user"); cnt = Integer.valueOf(bundle.getString("maxcount")); price = Long.valueOf(bundle.getString("price")); } public static void main(String[] args) { System.out.println("実行結果:"); System.out.println("username: " + user); System.out.println("count: " + cnt); System.out.println("password: " + price); } }
package com.arkgame.study;

import java.util.ResourceBundle;

public class BundeDemo {

      private static String user;
      private static int cnt;
      private static long price;

      static {
            ResourceBundle bundle = ResourceBundle.getBundle("common");
            user = bundle.getString("user");
            cnt = Integer.valueOf(bundle.getString("maxcount"));
            price = Long.valueOf(bundle.getString("price"));
      }

      public static void main(String[] args) {

            System.out.println("実行結果:");
            System.out.println("username: " + user);
            System.out.println("count: " + cnt);
            System.out.println("password: " + price);

      }
}

common.properties
user=testuser001
price=12345678
maxcount=10

実行結果:
username: testuser001
count: 10
password: 12345678

Java

Posted by arkgame