java中properties的使用实例

package com.ywx.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;
/**
 * 题目来源:项目中使用到的读取和写入xml配置文件
 * 作者:Vashon
 * 时间:20150407
 */
class XmlOperate{
	/**
	 * //写入xml文件
	 * @param file
	 * @param comment
	 */
	public static void write(File file,String comment){
		Properties pro=new Properties();
		pro.setProperty("BJ", "北京");
		pro.setProperty("SH", "上海");
		pro.setProperty("GZ", "广州");
		pro.setProperty("GY", "贵阳");
		pro.setProperty("WH", "武汉");
		pro.setProperty("CD", "成都");
		pro.setProperty("XA", "西安");
		pro.setProperty("HZ", "杭州");
		pro.setProperty("DL", "大连");
		try {
			pro.storeToXML(new FileOutputStream(file), comment);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	/**
	 * 读取xml文件
	 * @param file
	 */
	public static void read(File file,String key){
		Properties pro=new Properties();
		try {
			pro.loadFromXML(new FileInputStream(file));
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(key+"-->"+pro.getProperty(key));
	}
}
public class XmlTest {
	public static void main(String args[]){
		File file=new File("d:"+File.separator+"area.xml");
//		XmlOperate.write(file, "地区列表");
		XmlOperate.read(file,"BJ");
		XmlOperate.read(file,"SH");
	}
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

Stay Hungry, Stay Foolish, Walking in Life
原文地址:https://www.cnblogs.com/ywx-vashon/p/4895784.html