Hashtable(Properties)の内容をText-fileに保存する


import java.io.*;
import java.util.*;
public class PropertySet {
       public static void main(String[] args) {
           Properties  pp  = new Properties();
           pp.setProperty("FONT", "Dialog");
           pp.setProperty("SIZE", "22");
           pp.setProperty("COLOR", "BLUE");
           try {
                FileOutputStream fos  = new FileOutputStream("testprop2.txt");
                pp.store(fos, "This is comment");
                
           } catch (IOException ioe) {
                ioe.printStackTrace();
                System.exit(-1);
           }  
        }
}


<実行結果>

<FileOutputStream クラス>
ファイルにバイト単位でデータを書き込みます。

setPropertyメソッド>
Hashtableクラスのputメソッドと同じですが、コンパイル時に引数がString型であることをチェックしています。
JDK1.2から提供されたメソッドです。

storeメソッド>
Propertiesオブジェクトのリストをリストファイルに出力します。