首頁  >  文章  >  Java  >  關於Java中Properties的簡單介紹

關於Java中Properties的簡單介紹

黄舟
黄舟原創
2017-05-28 09:21:401996瀏覽

Java中有個比較重要的類別Properties(Java.util.Properties),主要用於讀取Java的設定檔,各種語言都有自己支援的設定文件,設定文件中很多變數是經常改變的,這樣做也是為了方便用戶,讓用戶能夠脫離程式本身去修改相關的變數設定

Java中Properties類別的操作

    知識學而不用,就等於沒用,到真正用到的時候還要重新再學。最近在看幾款開源模擬器的源碼,裡面涉及到了很多關於Properties類別的引用,由於Java已經好久沒用了,而這些模擬器大多用Java來寫,外加一些腳本語言Python,Perl之類的,不得已,又得重新拾起。

一、Java Properties類別

    Java中有個較重要的類別Properties(Java.util.Properties),主要用於讀取取Java的設定文件,各種語言都有自己所支援的設定文件,設定檔中很多變數是經常改變的,這樣做也是為了方便用戶,讓用戶能夠脫離程式本身去修改相關的變數設定。像Python支援的設定檔是.ini文件,同樣,它也有自己讀取配置文件的類別ConfigParse,方便程式設計師或使用者透過該類別的方法來修改.ini設定檔。在Java中,其設定檔常為.properties文件,格式為文字文件,文件的內容的格式是「鍵=值」的格式,文字註解訊息可以用"#"來註解。

Properties類別繼承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 (),清除所有裝載的 鍵 - 值對。該方法在基類中提供。

二、Java讀取Properties檔案

#    Java讀取Properties檔案的方法有很多但最常用的還是透過java .

lang.Class類別的getResourceAsStream(String name)方法來實現,如下可以這樣呼叫:

InputStream in = getClass().getResourceAsStream("资源Name");作为我们写程序的,用此一种足够。

或下面這種也常用:

InputStream in = new BufferedInputStream(new FileInputStream(filepath));

三、相關實例

#下面列舉幾個實例,加深對Properties類別的理解與記憶。

我們知道,Java虛擬機器(JVM)有自己的系統設定檔(system.properties),我們可以透過下面的方式來取得。

1、取得JVM的系統屬性

 import java.util.Properties;
 public class ReadJVM {
  public static void main(String[] args) {
   Properties pps = System.getProperties();
   pps.list(System.out);
  }
 }

結果:


##2 、隨便新建一個設定檔(Test.properties)

name=JJ
Weight=4444
Height=3333
 
 public class getProperties {
  public static void main(String[] args) throws FileNotFoundException, IOException {
   Properties pps = new Properties();
   pps.load(new FileInputStream("Test.properties"));
   Enumeration enum1 = pps.propertyNames();//得到配置文件的名字
   while(enum1.hasMoreElements()) {
    String strKey = (String) enum1.nextElement();
    String strValue = pps.getProperty(strKey);
    System.out.println(strKey + "=" + strValue);
   }
  }
 }

#3、一個比較綜合的實例

##根據key讀取value

#讀取properties的完整資訊

寫入新的properties資訊 

 //关于Properties类常用的操作
 public class TestProperties {
  //根据Key读取Value
  public static String GetValueByKey(String filePath, String key) {
   Properties pps = new Properties();
   try {
    InputStream in = new BufferedInputStream (new FileInputStream(filePath)); 
    pps.load(in);
    String value = pps.getProperty(key);
    System.out.println(key + " = " + value);
    return value;
    
   }catch (IOException e) {
    e.printStackTrace();
    return null;
   }
  }
  
  //读取Properties的全部信息
  public static void GetAllProperties(String filePath) throws IOException {
   Properties pps = new Properties();
   InputStream in = new BufferedInputStream(new FileInputStream(filePath));
   pps.load(in);
   Enumeration en = pps.propertyNames(); //得到配置文件的名字
   
   while(en.hasMoreElements()) {
    String strKey = (String) en.nextElement();
    String strValue = pps.getProperty(strKey);
    System.out.println(strKey + "=" + strValue);
   }
   
  }
  
  //写入Properties信息
  public static void WriteProperties (String filePath, String pKey, String pValue) throws IOException {
   Properties pps = new Properties();
   
   InputStream in = new FileInputStream(filePath);
   //从输入流中读取属性列表(键和元素对) 
   pps.load(in);
   //调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。 
   //强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
   OutputStream out = new FileOutputStream(filePath);
   pps.setProperty(pKey, pValue);
   //以适合使用 load 方法加载到 Properties 表中的格式, 
   //将此 Properties 表中的属性列表(键和元素对)写入输出流 
   pps.store(out, "Update " + pKey + " name");
  }
  
  public static void main(String [] args) throws IOException{
   //String value = GetValueByKey("Test.properties", "name");
   //System.out.println(value);
   //GetAllProperties("Test.properties");
   WriteProperties("Test.properties","long", "212");
  }
 }

结果:

Test.properties中文件的数据为:

#Update long name
#Sun Feb 23 18:17:16 CST 2016
name=JJ
Weight=4444
long=212
Height=3333

以上是關於Java中Properties的簡單介紹的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn