Java Properties 集合

/*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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章