or1ko's diary

日々を書きます

キーと値の設定(ファイル)の読み書き

キーと値だけの設定(ファイル)であれば、
java.uti.Propertiesクラスで簡単に作成できる。
XMLでも入出力できるようになっている。
日本語を含む場合、XMLで出力しておけば、エディタで開いても値がわかる。

import java.io.IOException;
import java.util.Properties;


public class SamplePropertiesIO {

	public static void main(String[] args) throws IOException {
		Properties properties = new Properties();
		properties.setProperty("key", "value");
		properties.store(System.out, "Settings");

		// 読み込む場合は、loadを呼ぶ
		// Properties p2 = new Properties();
		// p2.load(stream);
	}
}

実行結果。

#Settings
#Sun Mar 15 21:02:42 JST 2015
key=value