Properties的两个方法:
store(OutputStream out, String comments):持久化
load(InputStream inStream):加载
store(Writer writer, String comments)
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Properties; public class PropertyDemo { public static void main(String[] args) throws IOException { /* * 演示一下Properties的特有方法 */ methodDemo3(); } public static void methodDemo3() throws IOException { Properties prop = new Properties(); //定义读取流和数据文件关联 FileInputStream fis = new FileInputStream("tempfile\info.properties"); prop.load(fis); prop.setProperty("zhangsan", "12"); //要将改完的数据重新持久化 FileOutputStream fos = new FileOutputStream("tempfile\info.properties"); prop.store(fos, "flush data");//第二个参数是注释的意思 prop.list(System.out); fos.close(); fis.close(); } public static void methodDemo2() throws IOException{ Properties prop = new Properties(); prop.setProperty("zhangsan", "20"); prop.setProperty("lisi", "23"); prop.setProperty("wangwu", "10"); //将集合中的数据持久化到设备上 //需要输出流对象 FileOutputStream fos = new FileOutputStream("tempfile\info.properties"); //使用prop的store方法, prop.store(fos, "my demo person info");//store需要用到输出流 fos.close(); } public static void methodDemo(){ //Properties的基本存和取 //1.创建一个Properties Properties prop = new Properties(); prop.setProperty("zhangsan", "20"); prop.setProperty("lisi", "23"); prop.setProperty("wangwu", "10"); prop.list(System.out);//此方法对调试很有用 // Set<String> set = prop.stringPropertyNames(); // // for(String name:set){ // String value = prop.getProperty(name); // System.out.println(name+"...."+value); // } } }