保存属性至xml并读取

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.InvalidPropertiesFormatException;
import java.util.Properties;

public class PropertiesDemo04 {
    public static void main(String[] args) {
        Properties pro = new Properties();
        pro.setProperty("BJ", "Beijing");
        pro.setProperty("DJ", "TOKYO");
        pro.setProperty("DB", "oSA");
        File file = new File("E:" + File.separator + "area.xml");
        try {
            pro.storeToXML(new FileOutputStream(file), "AREA INFO");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            pro.loadFromXML(new FileInputStream(file));
        } catch (InvalidPropertiesFormatException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("DJ:" + pro.getProperty("DJ"));
    }
}

posted @ 2014-09-08 14:15  塔斯曼  阅读(143)  评论(0编辑  收藏  举报