Properties

Properties 介紹

Properties的父類別是Hashtable。

java.lang.Object
  java.util.Dictionary<K,V>
    java.util.Hashtable<Object,Object>
      java.util.Properties

Properties是一個類別。

使用Key=Value儲存。

注意!Key中間不能為空白,Value不能因為它是String,就使用"文字"雙引號。

常用方法

  • load 載入資源檔
  • list 將內容顯示到那裡
  • getProperty(key) 取得屬性
  • setProperty(key) 設定屬性
  • stroe 儲存資源檔

建立Properties

在src目錄下按滑鼠右鍵。
img

img

內容輸入

user=Bill
password=1234

建立Properties物件

1
Properties prop = new Properties();

windows的檔案路徑

src\\Test.properties

mac、linux的檔案路徑

src/Test.properties

讀取檔案

1
2
Properties prop = new Properties();
prop.load(new FileReader("src/Test.properties"));

取得屬性

1
2
3
4
5
6
7
8
9
10
public class Property {
  public static void main(String[] args) throws IOException {
    Properties prop = new Properties();
    prop.load(new FileReader("src/Test.properties"));
    String user = prop.getProperty("user");
    String password = prop.getProperty("password");
    System.out.println("user = " + user);
    System.out.println("password = " + password);
  }
}
user = Bill
password = 1234

設定屬性

語法

prop.setProperty("key","value");
1
2
3
Properties prop = new Properties();
prop.setProperty("user","Mary");
prop.setProperty("password","abcd");

屬性設完後,要儲存檔案。

prop.store(new FileOutputStream("檔案位置"), "註解");
prop.store(new FileOutputStream("檔案位置"), null);

若沒註解,可設為null。

完整程式碼

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

public class Property {
  public static void main(String[] args) throws IOException {
    Properties prop = new Properties();
    prop.load(new FileReader("src/Test.properties"));
    String user = prop.getProperty("user");
    String password = prop.getProperty("password");
    System.out.println("user = " + user);
    System.out.println("password = " + password);
    prop.setProperty("user","Mary");
    prop.setProperty("password","abcd");
    user = prop.getProperty("user");
    password = prop.getProperty("password");
    System.out.println("user = " + user);
    System.out.println("password = " + password);
    prop.store(new FileOutputStream("src/Test.properties"), null);
  }
}
user = Bill
password = 1234
user = Mary
password = abcd

修改過後的檔案內容。

#Fri Nov 21 11:27:15 CST 2025
password=abcd
user=Mary

建立Properties檔案

語法

prop.store(new FileOutputStream("檔案位置"),"註解")
prop.store(new FileOutputStream("檔案位置"), null)

若沒註解,可設為null。

建立Properties檔案

1
2
3
4
Properties prop = new Properties();
prop.setProperty("password","1234");
String comment = "create by cici";
prop.store(new FileOutputStream("src/Test2.properties"),comment);

產生的檔案內容

#create by cici
#Fri Nov 21 11:23:42 CST 2025
password=1234

results matching ""

    No results matching ""