public class PropertiesDemo {
public static void main(String[] args) throws Exception{
Properties pro = new Properties(); //声明属性集合对象
pro.setProperty("a", "one"); //设置键值
pro.setProperty("b", "two");
pro.setProperty("c", "three");
pro.setProperty("d", "four");
File f = new File("d:" + File.separator + "test.properties"); //设置文件路径
if(!f.exists()){//若文件不存在
pro.store(new FileOutputStream(f),"Info");//存放属性值到文件
}else {//若文文件存在
pro.load(new FileInputStream(f)); //从文件读取属性
}
}
}
结果为:
#Info
#Tue Sep 17 22:41:30 CST 2013
b=two
a=one
d=four
c=three
|