關於properties文件讀取

-----讀取-----

load(InputStream inStream)
這個方法可以從.properties屬性文件對應的文件輸入流中,加載屬性列表到Properties類對象
Properties prop = new Properties();
//讀取屬性文件test.properties(BufferedInputStream 輸入流)
InputStream in = new BufferedInputStream (new FileInputStream("test.properties"));
//加載屬性列表           
prop.load(in);
//遍歷所有key(屬性名)
Iterator<String> it=prop.stringPropertyNames().iterator();
while(it.hasNext()){
	String key=it.next();
	//獲取value(屬性值)
	System.out.println(key+":"+prop.getProperty(key));
}
//關閉流
in.close();

-----寫入-----

store(OutputStream out, String comments)
這個方法將Properties類對象的屬性列表保存到輸出流中
Properties prop = new Properties();
//保存屬性到test2.properties文件
FileOutputStream out = new FileOutputStream("test2.properties", true);//true表示追加打開
//設置value(屬性值)
prop.setProperty("phone", "10086");
//如果"The New properties file"不爲空,保存後的屬性文件第一行會是#The New properties file表示註釋信息;如果爲空則沒有註釋信息。
prop.store(out, "The New properties file");
//關閉流
out.close();

發佈了38 篇原創文章 · 獲贊 11 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章