import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;
public class ProperitesDemo {
/**
* hashtable 的子类,集合中和IO相结合的容器
* @throws IOException
* porp.txt 文件存放在 项目根路径下
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
getProperties1();
}
//读取文本文件 到 Properties
static void getProperties() throws IOException{
BufferedReader br = new BufferedReader(new FileReader("porp.txt"));
String line =null;
Properties prop = new Properties();
while((line=br.readLine())!=null){
String[] str = line.split("=");
prop.setProperty(str[0], str[1]);
System.out.println(line);
}
}
//读取文本文件 到 Properties
static void getProperties1() throws IOException{
FileInputStream in = new FileInputStream("porp.txt");
Properties prop = new Properties();
prop.load(in);
prop.setProperty("lisi", "100");
FileOutputStream out = new FileOutputStream("porp.txt");
prop.store(out, " motyfed by leog ");
in.close();
out.close();
//System.out.println(prop);
prop.list(System.out);
}
//读取和设置 Properties 中的键值对
static void getAndset(){
Properties prop = new Properties();
prop.setProperty("zhangsan", "28");
prop.setProperty("lishi", "26");
System.out.println(prop);
prop.setProperty("zhangsan", 30+"");
Set<String> set = prop.stringPropertyNames();
for(String s :set){
System.out.println(s+"="+prop.getProperty(s));
}
}
}