java操作.properties檔案

來源:互聯網
上載者:User

java中的properties檔案是一種設定檔,主要用於表達配置資訊,檔案類型為*.properties,格式為文字檔,檔案的內容格式是"鍵=值",在properties檔案中,可以用"#"來作注釋,properties檔案在Java編程中用到的地方很多,操作很方便。

一、Properties類的幾個重要方法

Properties類存在於java.util包下,該類繼承自Hashtable.

1. getProperty(String
key)  用指定的鍵在此屬性列表中搜尋屬性,也就是通過參數key,找到key所對應的value,如果未找到則返回null。

2. setProperty(String
key, String value)  調用Hashtable 的 put方法。傳回值是Hashtable
調用put 的結果。用於往.properties檔案中添加新的索引值對。

3. load(InputStream
inStream)  從輸入資料流中讀取屬性列表(鍵和元素對)。通過對指定的檔案.properties 檔案進行裝載來擷取該檔案中的所有鍵
- 值對。     以供
getProperty(String key)  來搜尋。

4. store(OutputStream
out, String comments)  以適合使用 load(InputStream inStream)方法載入到 Properties 表中的格式,將此 Properties 表中的屬性列表(鍵和元素對)寫入輸出資料流。與
load 方法相反,該方法將鍵 - 值對寫入到指定的檔案中去。

5.  clear()
 清空所有裝載的 鍵 - 值對。該方法在基類Hashtable<K,V>中提供。

二、在程式中如何操作.properties檔案

給出一份完整代碼,供參考

package com.properties;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.util.Enumeration;import java.util.Properties;public class TestProperties {/** * 通過key讀取value的值 *  * @param keyName *            鍵的名稱 * @param filePath *            需要讀取的檔案路徑 * @return 鍵所對應的值 */public static Object getKeyInfo(String filePath, String keyName) {InputStream is = TestProperties.class.getClassLoader().getResourceAsStream(filePath);Properties pro = new Properties();try {pro.load(is);} catch (IOException e) {e.printStackTrace();} finally {try {is.close();is = null;} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}return pro.get(keyName);}/** * 往properties中寫入索引值對 *  * @param filePath *            需要寫入的檔案路徑 * @param keyName *            key值 * @param value *            value值 */public static void SetProperties(String filePath, String keyName,String value) {InputStream is = TestProperties.class.getClassLoader().getResourceAsStream(filePath);OutputStream os = null;Properties pro = new Properties();try {pro.load(is);os = new FileOutputStream(filePath);pro.setProperty(keyName, value);pro.store(os, "Update '" + keyName + "' value");} catch (IOException e) {e.printStackTrace();} finally {try {is.close();os.close();is = null;os = null;} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}/** * 讀取properties檔案中的所有索引值對 *  * @param filePath *            檔案路徑 */public static void readProperties(String filePath) {Properties props = new Properties();try {InputStream in = TestProperties.class.getClassLoader().getResourceAsStream(filePath);props.load(in);Enumeration en = props.propertyNames();while (en.hasMoreElements()) {String key = (String) en.nextElement();String Property = props.getProperty(key);System.out.println(key + Property);}} catch (Exception e) {e.printStackTrace();}}public static void main(String[] args) {TestProperties.getKeyInfo("testPro.properties", "A");TestProperties.SetProperties("testPro.properties", "C", "33333");readProperties("src\testPro.properties");}}

補充:.properties檔案 一般放在src根目錄下或自己在src下建一個目錄存放,當然放在其他地方可以。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.