Properties的load()和store()

package io;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.Properties;

public class Io3 {
	public static void main(String[] args) throws IOException {
		myStore();
	}
	public static void  myLoad() throws IOException{
		Properties prop = new Properties();
		Reader r = new FileReader("prop.txt");	//文件中的内容必须是键值对形式
		prop.load(r);
		r.close();
		
		System.out.println(prop);
	}
	public static void myStore() throws IOException{
		Properties prop = new Properties();
		prop.setProperty("张三", "zhangsan");
		prop.setProperty("李四", "lisi");
		prop.setProperty("王五", "wangwu");
		Writer w = new FileWriter("prop.txt");
		prop.store(w, "comments");
		w.close();
	}
}

store()方法生成的文件如下图:

Properties的load()和store()_第1张图片 

load()方法输出的内容如下图:

Properties的load()和store()_第2张图片 

 

你可能感兴趣的:(Properties的load()和store())