JAva propertiesリソースファイル-1を読み込む

1299 ワード

JAVAがリソースファイルを読み込む2つの方法
ファイルはすべて*.propertiesを拡張子としてclasspathで読み込みます. 
 
1つ目:
java.util.PropertyResourceBundle  
java.util.ResourceBundle
ResourceBundle bundle = PropertyResourceBundle.getBundle("config")
String name = bundle.getString("name");
String company = bundle.getString("company");

 
 
2つ目:
パッケージ:java.util.Properties
継承方法は次のとおりです. Properties -> hashtable ->Dictionay      
Properties  pros = new Properties();  
FileInputStream fis = new FileInputStream(TestProperty.class.getClassLoader().getResource("config.properties").getPath());
pros.load(fis);
String name = pros.getProperty("name");
String company = pros.getProperty("company");

 
 
共通点:
1.keyの列挙集合を返すことができる
Enumeration<String> key =  bundle.getKeys();
while(key.hasMoreElements()){
System.out.println(" bundle = " +  key.nextElement() + "--" +                      bundle.getString(key.nextElement()));
}

 
2.ある containsKey(key)  キーメソッドがブール値を返すかどうかを判断します
3.ある bundle.keySet()は、Setコレクションを返します.