Javaプロパティファイルの例
通常、Javaプロパティファイルは、プロジェクト構成データまたは設定を格納するために使用されます。このチュートリアルでは、プロパティファイルの読み書き方法を説明します。
1.プロパティファイルに書き込む
プロパティ値を設定し、 `config.properties`という名前のプロパティファイルに書き込みます。
App.java
package com.mkyong.properties; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStream; import java.util.Properties; public class App { public static void main(String[]args) { Properties prop = new Properties(); OutputStream output = null; try { output = new FileOutputStream("config.properties"); //set the properties value prop.setProperty("database", "localhost"); prop.setProperty("dbuser", "mkyong"); prop.setProperty("dbpassword", "password"); //save properties to project root folder prop.store(output, null); } catch (IOException io) { io.printStackTrace(); } finally { if (output != null) { try { output.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
出力
config.properties
#Fri Jan 17 22:37:45 MYT 2014 dbpassword=password database=localhost dbuser=mkyong
__P.Sファイルパスが指定されていない場合、このプロパティファイルはプロジェクトのルートフォルダに保存されます。
2.プロパティファイルをロードする
ファイルシステムからプロパティファイルをロードし、プロパティ値を取得します。
App.java
package com.mkyong.properties; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class App { public static void main(String[]args) { Properties prop = new Properties(); InputStream input = null; try { input = new FileInputStream("config.properties"); //load a properties file prop.load(input); //get the property value and print it out System.out.println(prop.getProperty("database")); System.out.println(prop.getProperty("dbuser")); System.out.println(prop.getProperty("dbpassword")); } catch (IOException ex) { ex.printStackTrace(); } finally { if (input != null) { try { input.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
出力
localhost mkyong password
3.クラスパスからプロパティファイルをロードする
プロジェクトクラスパスからプロパティファイル `config.properties`を読み込み、プロパティ値を取得します。
__P.Sプロパティファイル “config.properties”がプロジェクトのクラスパスルートフォルダにあるとします。
App.java
package com.mkyong.properties; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class App { public static void main( String[]args ){ Properties prop = new Properties(); InputStream input = null; try { String filename = "config.properties"; input = App3.class.getClassLoader().getResourceAsStream(filename); if(input==null){ System.out.println("Sorry, unable to find " + filename); return; } //load a properties file from class path, inside static method prop.load(input); //get the property value and print it out System.out.println(prop.getProperty("database")); System.out.println(prop.getProperty("dbuser")); System.out.println(prop.getProperty("dbpassword")); } catch (IOException ex) { ex.printStackTrace(); } finally{ if(input!=null){ try { input.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
出力
localhost mkyong password
非静的メソッドの場合は、次のようにします。
prop.load(getClass().getClassLoader().getResourceAsStream("config.properties"));
4.プロパティファイルからすべてを印刷します.
プロジェクトクラスパスからプロパティファイル `config.properties`を読み込み、キーと値を取得します。
App.java
package com.mkyong.properties; import java.io.IOException; import java.io.InputStream; import java.util.Enumeration; import java.util.Properties; public class App { public static void main(String[]args) { App app = new App(); app.printThemAll(); } private void printThemAll() { Properties prop = new Properties(); InputStream input = null; try { String filename = "config.properties"; input = getClass().getClassLoader().getResourceAsStream(filename); if (input == null) { System.out.println("Sorry, unable to find " + filename); return; } prop.load(input); Enumeration<?> e = prop.propertyNames(); while (e.hasMoreElements()) { String key = (String) e.nextElement(); String value = prop.getProperty(key); System.out.println("Key : " + key + ", Value : " + value); } } catch (IOException ex) { ex.printStackTrace(); } finally { if (input != null) { try { input.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
出力
Key : dbpassword, Value : password Key : database, Value : localhost Key : dbuser, Value : mkyong
参考文献
-
リンク://java/java-getresourceas-static-method-stream/[Java:
静的メソッドのGetResourceAsStream]。
http://docs.oracle.com/javase/7/docs/api/java/util/Properties.html
[Java
プロパティJavaDoc]