1. 程式人生 > 其它 >屬性對映(property map)

屬性對映(property map)

技術標籤:筆記java

屬性對映(property map)

1.Properties類是什麼?

Properties(Java.util.Properties),該類主要用於讀取Java的配置檔案,不同的程式語言有自己所支援的配置檔案,配置檔案中很多變數是經常改變的,為了方便使用者的配置,能讓使用者夠脫離程式本身去修改相關的變數設定。就像在Java中,其配置檔案常為.properties檔案,是以鍵值對的形式進行引數配置的。

是一個特殊型別的對映結構

  • **鍵與值都是字串
  • 這個對映可以很容易的儲存到檔案以及從檔案載入**
  • 有一個二級表存放預設值

構造器

		Properties()
       建立一個無預設值的空屬性列表。

		Properties(Properties defaults)
        建立一個帶有指定預設值的空屬性列表。

常用方法

 String	getProperty(String key)
      用指定的鍵在此屬性列表中搜索屬性。

String	getProperty(String key, String defaultValue)
      用指定的鍵在屬性列表中搜索屬性。

Object setProperty(String key, String value)
      呼叫 Hashtable 的方法 put。
      
void    store(OutputStream out, String comments) 將此Properties表中的此屬性列表(鍵和元素對)以適合使用load(InputStream)方法載入到Properties表的格式寫入輸出流。 此Properties方法不會寫出此Properties表的defaults表中的屬性(如果有)。

建立一個properties集合並列印

public static void writerToProperties()
    {
        //建立properties集合(不支援泛型<>)
        Properties p=new Properties();
        
        //鍵值對寫入集合p
        p.setProperty("url","cn.WriterPracticeNode");
        p.setProperty("user","11171028");
        p.setProperty("password","1028");
        
        //列印集合
        System.out.println(p);
    }

執行結果:

列印輸出結果

用指定的鍵獲取對應的值

 //用指定的鍵獲取對應的值
        System.out.println(p.getProperty("user"));

會獲得 11171028

將Properties集合中的鍵值對寫入到檔案中(store方法)

  //建立properties集合
        Properties p=new Properties();
        //鍵值對寫入集合p
        p.setProperty("url","cn.WriterPracticeNode");
        p.setProperty("user","11171028");
        p.setProperty("password","1028");
        //建立轉換流   "test01_1_17.properties" 就是建立的檔名
            OutputStream out=null;
        try {
            out = new BufferedOutputStream(new FileOutputStream("test01_1_17.properties"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        //store 方法第二個引數 practiceNode是你要建立的檔案裡面第一句註解
        try {
            p.store(out,"practiceNode");
        } catch (IOException e) {
            e.printStackTrace();
        }

將Properties集合中的鍵值對寫入到檔案中(list方法)
//屬性集合類不支援泛型

    Properties prop = new Properties();

    //新增鍵值對

    prop.setProperty("name", "zhangsan");

    prop.setProperty("age", "10");

    prop.setProperty("gender", "male");

    PrintWriter pw = null;

    try {

       //建立自動重新整理字元列印流物件

       pw = new PrintWriter(new FileWriter("prop1.txt"),true);

       //使用list()方法,把prop中的鍵值對寫入到檔案中

       prop.list(pw);

    } catch (IOException e) {

       e.printStackTrace();

    }finally {

       if(pw != null)

       pw.close();

    }

}

讀取properties方式

一、 使用java.util.Properties類的load(InputStream in)方法載入properties檔案

public static String getPath1() {
        InputStream in=InputStream.nullInputStream();
        try {
            in=new BufferedInputStream(new FileInputStream(basePath));
            /*"E:\\Java程式碼練習檔案\\src\\com\\javase\\practiceNode\\Java\\propertiesPractice\\java\\practice.properties"*/
            Properties p=new Properties();
            p.load(in);
            password=p.getProperty("password");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  password;
        }

用ResourceBundle類的getBundle()方法

注意:這個getBundle()方法的引數只能寫成包路徑+properties檔名,否則將拋異常

//com/javase/practiceNode/Java/propertiesPractice/java/practice
return ResourceBundle.getBundle(basePath).getString("password");

用PropertyResourceBundle類的建構函式

PropertyResourceBundle p = null;
        try {
            p= new PropertyResourceBundle(new FileInputStream(basePath));
            password= p.getString("password");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return password;

使用java.lang.ClassLoader類載入器的getSystemResourceAsStream()靜態方法

//"com/javase/practiceNode/Java/propertiesPractice/java/practice.properties"
InputStream in = ClassLoader.getSystemResourceAsStream(basePath);
        try {

            Properties propertie=new Properties();
            propertie.load(in);
            password=propertie.getProperty("password");

        } catch (IOException e) {
            e.printStackTrace();
        }
        return password;