1. 程式人生 > 實用技巧 >Java日誌第39天 2020.8.13

Java日誌第39天 2020.8.13

Properties

java.util.Properties extends Hashtable<k, v> implements Map<k, v>

Properties表示了一個持久的屬性集。Properties可儲存在流中或從流中載入。

Properties集合是唯一和IO流相結合的集合

  可以使用Properties集合中的方法store,把集合中的臨時資料持久化寫入到硬碟中儲存

  可以使用Properties集合中的方法load,把硬碟中儲存的檔案(鍵值對)讀取到集合中使用

屬性列表中每個鍵及其對應值都是一個字串。

  Properties集合是一個雙列集合,key和value預設都是字串

import java.util.Properties;
import java.util.Set;

public class Demo01Properties {

    public static void main(String[] args) {
        //建立Properties集合物件
        Properties prop = new Properties();
        //使用setProperty往集合中新增資料
        prop.setProperty("迪麗熱巴","165");
        prop.setProperty("古力娜扎","164");
        prop.setProperty(
"馬爾扎哈","163"); prop.setProperty("撒揚娜拉","162"); //使用stringPropertyName把集合中能夠的鍵取出,儲存至一個Set集合中 Set<String> set = prop.stringPropertyNames(); //遍歷set集合,取出Properties集合的每一個鍵 for (String key : set) { String value = prop.getProperty(key); System.out.println(key
+ "=" + value); } } }

store方法

- void store(OutputStream out, String comments)

- void store(Writer writer, String comments)

引數:

OutputStream out:位元組輸出流,不能寫入中文

Writer writer:字元輸出流,可以寫中文

String comments:註釋,用來解釋說明儲存的檔案是做什麼用的

  不能使用中文,會產生亂碼,因為預設是Unicode編碼

  一般使用“”空字串

使用步驟:

1.建立Properties集合物件,新增資料

2.建立位元組輸出流/字元輸出流物件,構造方法中要繫結輸出的路徑

3.使用Properties集合中的方法store,把集合中的臨時資料持久化寫入到硬碟中儲存

4.釋放資源

import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;

public class Demo02Properties {
    public static void main(String[] args) throws IOException {
        //建立Properties集合物件,新增資料
        Properties prop = new Properties();
        prop.setProperty("迪麗熱巴","165");
        prop.setProperty("古力娜扎","164");
        prop.setProperty("馬爾扎哈","163");
        prop.setProperty("撒揚娜拉","162");
        //建立位元組輸出流/字元輸出流物件,構造方法中要繫結輸出的路徑
        FileWriter fw = new FileWriter("E:\\Java\\Practice\\src\\Practice\\demo15\\d.txt");

        //使用Properties集合中的方法store,把集合中的臨時資料持久化寫入到硬碟中儲存
        prop.store(fw,"save data");
        
        //釋放資源
        fw.close();
    }
}

結果如下:

load方法

- void load(InputStream inStream)

- void load(Reader reader)

引數:

InputStream inStream:位元組輸入流,不能讀取含有中文的鍵值對

Reader reader:字元輸入流,能讀取含有中文的鍵值對

使用步驟:

1.建立Properties集合物件

2.使用Properties集合物件中的方法load讀取儲存鍵值對的檔案

3.遍歷Properties集合

注意:

1.儲存鍵值對的檔案,鍵與值預設的連線符號可以使用=,空格

2.儲存鍵值對的檔案,可以使用#進行註釋,被註釋的鍵值對不會再被讀取

3.儲存鍵值對的檔案,鍵與值預設都是字串,不用再加引號

import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;

public class Demo03Properties {
    public static void main(String[] args) throws IOException {
        //建立Properties集合物件
        Properties prop = new Properties();

        //使用Properties集合物件中的方法load讀取儲存鍵值對的檔案
        prop.load(new FileReader("E:\\Java\\Practice\\src\\Practice\\demo15\\d.txt"));

        //遍歷Properties集合
        Set<String> set = prop.stringPropertyNames();

        for (String key : set) {
            String value = prop.getProperty(key);
            System.out.println(key + "=" + value);
        }
    }

結果如下: