/*Properties 集合
*
- 构造方法
- Properties () 创建一个Properties 集合对象
- 成员方法
- String getProperty(String key) 通过键获取值
- void load(InputStream inStream)从流中读取文件内容
- Object setProperty(String Key,String value)像集合中添加数据
- void store(OutputStram out,String comments)将集合中的数据通过流写出文件中
- 只能是字符串类型
*/
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
public class demo01 {
/*Properties 集合
*
* 构造方法
* Properties () 创建一个Properties 集合对象
* 成员方法
* String getProperty(String key) 通过键获取值
* void load(InputStream inStream)从流中读取文件内容
* Object setProperty(String Key,String value)像集合中添加数据
* void store(OutputStram out,String comments)将集合中的数据通过流写出文件中
* 只能是字符串类型
*/
public static void main(String[] args) throws IOException {
//创建Properties集合对象
Properties prop=new Properties();
// //像集合中添加数据
// prop.setProperty("usename", "admin");
// prop.setProperty("password", "123456");
// //获取集合中的数据
// String username=prop.getProperty("usename");
// String password=prop.getProperty("password");
// System.out.println(username+"===="+password);
prop.load(new FileInputStream("config.properties"));
String username=prop.getProperty("username");
String password=prop.getProperty("password");
System.out.println(username);
System.out.println(password);
prop.setProperty("tel", "188823456");
prop.store(new FileOutputStream ("config.properties"), "update config");
}
}
在内个项目下建立一个file
#update config
#Fri Apr 17 19:04:42 GMT+08:00 2020
password=123456
tel=188823456
username=admin