JAVA | 56 - 類集框架 | Properties |

Properties 是 Hsahtable 的子類,主要是進行屬性的操作(屬性的最大特點是利用字符串設置 key 和 value)。

在使用 Properties 類的時候,不需要設置泛型類型,因爲它一開始出現就只能保存 String。

資源文件的特點是:key = value

資源文件中的數據一定都是字符串。

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;

public class Main {
    public static void main(String[] args) throws Exception{
        // 設置了泛型,從而保證集合中所有的數據類型都一致
        Properties properties = new Properties();
        properties.setProperty("BJ","北京");
        properties.setProperty("TJ","天津");
        properties.setProperty("GD","廣東");
        System.out.println(properties.getProperty("TJ"));
        System.out.println(properties.getProperty("SH", "沒有此記錄"));

        File file = new File("/Users/yuzhen/File/area.properties");
        properties.store(new FileOutputStream(file),"area info");

        Properties properties1 = new Properties();
        properties1.load(new FileInputStream(file));
        System.out.println(properties1.getProperty("GD"));
    }
}
發佈了111 篇原創文章 · 獲贊 3 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章