.properties(或者.ini)文件的讀寫操作!

與XML一樣都是配置文件,一般簡單的,沒有結構的用properties,複雜的,有結構的用XML就行了:

讀操作:
[code]
File f = new File("abc.properties");//這個也可以改成.ini的文件
FileInputStream fin = new FileInputStream(f);
Properties proc = new Properties();
proc.load(fin);
String s = proc.getProperty("name");
System.out.println(s);//可以得到123
fin.close();
[/code]

abc.properties文件:
一般都是用鍵值對來表示
[code]
#Fri May 18 16:22:07 CST 2007
name=123
[/code]

寫操作:
[code]
File f = new File("abc.properties");
FileInputStream fin = new FileInputStream(f);
FileOutputStream out = new FileOutputStream(f);
Properties proc = new Properties();
proc.load(fin);
proc.setProperty("name", "123");
proc.store(out,"This is comment");
out.close();
fin.close();
[/code]
abc.properties文件:
[code]
#This is comment
#Fri May 18 16:28:37 CST 2007
name=123
[/code]
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章