java 讀取Properities 檔案

來源:互聯網
上載者:User

標籤: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 檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.