標籤:int close not 包括 輸出資料流 exception span 表示 system
今天看了下java的Properties 的這個東東,平時都在上班,忙捏。
基本屬性
1)load(InputStream inStream)
這個方法可以從.properties屬性檔案對應的檔案輸入資料流中,載入屬性列表到Properties類對象。如下面的代碼:
Properties pro = new Properties();FileInputStream in = new FileInputStream("a.properties");pro.load(in);in.close();
getProperty(String key)
setProperty(String key,String value)
store(OutputStream out, String comments)
這個方法將Properties類對象的屬性列表儲存到輸出資料流中。如下面的代碼:
FileOutputStream oFile = new FileOutputStream(file, true);pro.store(oFile, "Comment"); Comment 可以產生評論呢oFile.close();
publicclass PropertyTest {
public static void main(String[] args) throws FileNotFoundException, IOException {
// TODO Auto-generated method stub
Properties pro=new Properties();
FileInputStream f=new FileInputStream("config.properties");
pro.load(f);
// Set<String> stringPropertyNames()
//返回此屬性列表中的鍵集,其中該鍵及其對應值是字串,如果在主屬性列表中未找到同名的鍵,則還包括預設屬性列表中不同的鍵。
Iterator<String> s=pro.stringPropertyNames().iterator();
while(s.hasNext()){
String key=s.next();
System.out.println(key +" "+pro.getProperty(key));
}
f.close();
//儲存Properties檔案 寫到輸出資料流中去了
FileOutputStream oFile=new FileOutputStream("b.properties",true);//表示檔案可追加呢
pro.setProperty("phone", "10086");
pro.store(oFile,"The new Properties");//產生評論了
oFile.close();
}
java 讀取Properities 檔案