Properties的load()和store()功能
- A:Properties的load()和store()功能
- B:案例演示
- Properties的load()和store()功能
package com.heima.otherio;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;
public class Demo10_Properties {
/**
* @param args
* Properties是Hashtable的子类
* @throws IOException
* @throws FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException, IOException {
Properties prop = new Properties();
System.out.println("读取前 : " + prop);
//将文件上的键值对读取到集合中
prop.load(new FileInputStream("config.properties"));
//这里面修改,是改的内存里面的,并没有把改过内存里面的东西重新写到配置文件中.
prop.setProperty("tel", "18912345678");
System.out.println("读取后 : " + prop);
//第二个参数是对列表参数的描述,可以给值,也可以给null
prop.store(new FileOutputStream("config.properties"), null);
System.out.println(prop);
}
}
config.properties或者config.txt皆可.
qq=12345
tel=18912345678
username=zhangsan
或者
qq:12345
tel:18912345678
username:zhangsan