JAVA-properties類學習(

來源:互聯網
上載者:User
 

在我們平時寫程式的時候,有些參數是經常改變的,而這種改變不是我們預知的。比如說我們開發了一個操作資料庫的模組,在開發的時候我們串連本地的資料庫那麼 IP ,資料庫名稱,表名稱,資料庫主機等資訊是我們本地的,要使得這個操作資料的模組具有通用性,那麼以上資訊就不能寫死在程式裡。通常我們的做法是用設定檔來解決。

各種語言都有自己所支援的設定檔類型。比如 Python ,他支援 .ini 檔案。因為他內部有一個 ConfigParser 類來支援 .ini 檔案的讀寫,根據該類提供的方法程式員可以自由的來操作 .ini 檔案。而在 Java 中, Java 支援的是 .properties 檔案的讀寫。 JDK 內建的 java.util.Properties 類為我們操作 .properties 檔案提供了便利。

一. .properties 檔案的形式

==========================================================

# 以下為伺服器、資料庫資訊

dbPort = localhost

databaseName = mydb

dbUserName = root

dbPassword = root

# 以下為資料庫表資訊

dbTable = mytable

# 以下為伺服器資訊

ip = 192.168.0.9

······

在上面的檔案中我們假設該檔案名稱為: test.properties 檔案。其中 # 開始的一行為注釋資訊;在等號“ = ”左邊的我們稱之為 key ;等號“ = ”右邊的我們稱之為 value 。(其實就是我們常說的鍵 - 值對) key 應該是我們程式中的變數。而 value 是我們根據實際情況配置的。

二. JDK 中的 Properties 類

Properties 類存在於胞 Java.util 中,該類繼承自 Hashtable ,它提供了幾個主要的方法:

1. getProperty ( String  key) ,   用指定的鍵在此屬性列表中搜尋屬性。也就是通過參數 key ,得到 key 所對應的 value 。

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

3. setProperty ( String  key, String  value) ,調用 Hashtable 的方法 put 。他通過調用基類的put方法來設定 鍵 - 值對。

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

5. clear () ,清除所有裝載的 鍵 - 值對。該方法在基類中提供。

有了以上幾個方法我們就可以對 .properties 檔案進行操作了!

三.代碼執行個體

  package configuration;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class Configuration
...{
    private Properties propertie;
    private FileInputStream inputFile;
    private FileOutputStream outputFile;
   
    public Configuration()
    ...{
        propertie = new Properties();
    }
   
    public Configuration(String filePath)
    ...{
        propertie = new Properties();
        try ...{
            inputFile = new FileInputStream(filePath);
            propertie.load(inputFile);
            inputFile.close();
        } catch (FileNotFoundException ex) ...{
            System.out.println("讀取屬性檔案--->失敗!- 原因:檔案路徑錯誤或者檔案不存在");
            ex.printStackTrace();
        } catch (IOException ex) ...{
            System.out.println("裝載檔案--->失敗!");
            ex.printStackTrace();
        }
    }//end ReadConfigInfo(...)
   
    public String getValue(String key)
    ...{
        if(propertie.containsKey(key))...{
            String value = propertie.getProperty(key);//得到某一屬性的值
            return value;
        }
        else
            return "";
    }//end getValue(...)
   
    public String getValue(String fileName, String key)
    ...{
        try ...{
            String value = "";
    

相關文章

聯繫我們

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