1. 程式人生 > >JAVA--properties的基本用法

JAVA--properties的基本用法

java中的properties檔案是一種配置檔案,主要用於表達配置資訊,檔案型別為*.properties,格式為文字檔案,檔案的內容是格式是"鍵=值"的格式,在properties

檔案中,可以用"#"來作註釋,properties檔案在Java程式設計中用到的地方很多,操作很方便。
一、properties檔案

test.properties
------------------------------------------------------
#################################
#   工商報表應用IcisReport的配置檔案#
#   日期:2006年11月21日 #
#################################
#
#   說明:業務系統TopIcis和報表系統IcisReport是分離的
#   可分開部署到不同的伺服器上,也可以部署到同一個服務
#   器上;IcisReprot作為獨立的web應用程式可以使用任何
#   的Servlet容器或者J2EE伺服器部署並單獨執行,也可以
#   通過業務系統的介面呼叫作為業務系統的一個庫來應用.
#
#   IcisReport的ip
IcisReport.server.ip=192.168.3.143
#   IcisReport的埠
IcisReport.server.port=8080
#   IcisReport的上下文路徑
IcisReport.contextPath=/IcisReport

------------------------------------------------------ 
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檔案的java方法 

讀屬性檔案
Properties prop = new Properties();
InputStream in = getClass().getResourceAsStream("/IcisReport.properties
");
prop.load(in);
Set keyValue = prop.keySet();
for (Iterator it = keyValue.iterator(); it.hasNext();)
{
String key = (String) it.next();
}
------------------------
outputFile = new FileOutputStream(fileName);
propertie.store(outputFile, description);
outputFile.close();
-----------------------------------------------------------------------------------------
Class.getResourceAsStream ("/some/pkg/resource.properties");
ClassLoader.getResourceAsStream ("some/pkg/resource.properties");
java.util.ResourceBundle rs = java.util.ResourceBundle.getBundle("some.pkg.resource");
rs.getString("xiaofei");
-----------------------------------------------------------------------------------------
寫屬性檔案
Configuration saveCf = new Configuration();
saveCf.setValue("min", "10");
saveCf.setValue("max", "1000");
saveCf.saveFile(".\config\save.perperties","test");

總結:javaproperties檔案需要放到classpath下面,這樣程式才能讀取到,有關classpath實際上就是java類或者庫的存放路徑,在java工程中,properties放到

class檔案一塊。在web應用中,最簡單的方法是放到web應用的WEB- INF\classes目錄下即可,也可以放在其他資料夾下面,這時候需要在設定classpath環境變數的

時候,將這個資料夾路徑加到 classpath變數中,這樣也也可以讀取到。在此,你需要對classpath有個深刻理解,classpath絕非系統中刻意設定的那個系統環境變

量,WEB-INF\classes其實也是,java工程的class檔案目錄也是。

發個例子大家自己看哈.
package control;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
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 TestMain {
 
 //根據key讀取value
 public static String readValue(String filePath,String key) {
  Properties props = new Properties();
        try {
         InputStream in = new BufferedInputStream (new FileInputStream(filePath));
         props.load(in);
         String value = props.getProperty (key);
            System.out.println(key+value);
            return value;
        } catch (Exception e) {
         e.printStackTrace();
         return null;
        }
 }
 
 //讀取properties的全部資訊
    public static void readProperties(String filePath) {
     Properties props = new Properties();
        try {
         InputStream in = new BufferedInputStream (new FileInputStream(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();
        }
    }

    //寫入properties資訊
    public static void writeProperties(String filePath,String parameterName,String parameterValue) {
     Properties prop = new Properties();
     try {
      InputStream fis = new FileInputStream(filePath);
            //從輸入流中讀取屬性列表(鍵和元素對)
            prop.load(fis);
            //呼叫 Hashtable 的方法 put。使用 getProperty 方法提供並行性。
            //強制要求為屬性的鍵和值使用字串。返回值是 Hashtable 呼叫 put 的結果。
            OutputStream fos = new FileOutputStream(filePath);
            prop.setProperty(parameterName, parameterValue);
            //以適合使用 load 方法載入到 Properties 表中的格式,
            //將此 Properties 表中的屬性列表(鍵和元素對)寫入輸出流
            prop.store(fos, "Update '" + parameterName + "' value");
        } catch (IOException e) {
         System.err.println("Visit "+filePath+" for updating "+parameterName+" value error");
        }
    }

    public static void main(String[] args) {
     readValue("info.properties","url");
        writeProperties("info.properties","age","21");
        readProperties("info.properties" );
        System.out.println("OK");
    }