欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

深入理解Java中的Properties類

 更新時間:2024年01月30日 09:09:12   作者:碼農(nóng)研究僧  
Properties類是Java中用于處理配置文件的工具類,它繼承自 Hashtable類,實現(xiàn)了Map接口,本文主要介紹了Java中的Properties類,感興趣的可以了解一下

前言

使用Properties出現(xiàn)中文亂碼可看我這篇文章:properties出現(xiàn)中文亂碼解決方法(萬能)

1. 基本知識

Properties 類是 Java 中用于處理配置文件的工具類,它繼承自 Hashtable 類,實現(xiàn)了 Map 接口。

  • 主要用于讀取和寫入屬性文件,以鍵值對的形式存儲數(shù)據(jù)。
  • 配置文件通常以 .properties 為擴展名,其中每一行表示一個屬性或配置項。

使用該類可以更好的處理文件:

  • 配置文件管理: 主要用于讀取和保存應(yīng)用程序的配置信息,例如數(shù)據(jù)庫連接信息、用戶設(shè)置等。
  • 國際化: 用于加載不同語言的資源文件,方便國際化應(yīng)用程序。
  • 持久化: 可以將配置信息持久化到文件,以便下次程序啟動時重新加載。

對于其方法可查看其API接口:Properties API接口

構(gòu)造方法如下:

方法表述
Properties()創(chuàng)建一個沒有默認值的空屬性列表。
Properties?(int initialCapacity)創(chuàng)建一個沒有默認值的空屬性列表,并且初始大小容納指定數(shù)量的元素,而無需動態(tài)調(diào)整大小。
Properties?(Properties defaults)創(chuàng)建具有指定默認值的空屬性列表。

常用的方法如下:

返回類型方法表述
StringgetProperty?(String key)
getProperty?(String key, String defaultValue)
在此屬性列表中搜索具有指定鍵的屬性。
voidlist?(PrintStream out)
list?(PrintWriter out)
將此屬性列表打印到指定的輸出流。
voidload?(InputStream inStream)從輸入字節(jié)流中讀取屬性列表(鍵和元素對)。
voidload?(Reader reader)以簡單的面向行的格式從輸入字符流中讀取屬性列表(鍵和元素對)。
ObjectsetProperty?(String key, String value)調(diào)用 Hashtable方法 put 。
voidstore?(OutputStream out, String comments)將此 Properties表中的此屬性列表(鍵和元素對)以適合使用 load(InputStream)方法加載到 Properties表的格式寫入輸出流。
voidstore?(Writer writer, String comments)將此 Properties表中的此屬性列表(鍵和元素對)以適合使用 load(Reader)方法的格式寫入輸出字符流。

2. 代碼示例

當(dāng)使用 Properties 類時,你可以使用上述方法來讀取和寫入屬性。以下是這些方法的一些簡單的代碼示例:

1. getProperty(String key)

Properties properties = new Properties();
try (InputStream input = new FileInputStream("config.properties")) {
    properties.load(input);

    // 獲取屬性值
    String value = properties.getProperty("key");
    System.out.println("Value for key 'key': " + value);
} catch (IOException e) {
    e.printStackTrace();
}

2. getProperty(String key, String defaultValue)

Properties properties = new Properties();
try (InputStream input = new FileInputStream("config.properties")) {
    properties.load(input);

    // 獲取屬性值,如果不存在則使用默認值
    String value = properties.getProperty("nonexistentKey", "default");
    System.out.println("Value for key 'nonexistentKey': " + value);
} catch (IOException e) {
    e.printStackTrace();
}

3. list(PrintStream out) / list(PrintWriter out)

Properties properties = new Properties();
try (InputStream input = new FileInputStream("config.properties")) {
    properties.load(input);

    // 打印屬性列表到控制臺
    properties.list(System.out);
} catch (IOException e) {
    e.printStackTrace();
}

4. load(InputStream inStream)

Properties properties = new Properties();
try (InputStream input = new FileInputStream("config.properties")) {
    // 從輸入流中讀取屬性列表
    properties.load(input);

    // 遍歷所有鍵值對
    for (String key : properties.stringPropertyNames()) {
        String value = properties.getProperty(key);
        System.out.println(key + ": " + value);
    }
} catch (IOException e) {
    e.printStackTrace();
}

5. store(OutputStream out, String comments)

Properties properties = new Properties();
properties.setProperty("key1", "value1");
properties.setProperty("key2", "value2");

try (OutputStream output = new FileOutputStream("output.properties")) {
    // 將屬性列表寫入輸出流
    properties.store(output, "Comments for the output file");
} catch (IOException e) {
    e.printStackTrace();
}

6. store(Writer writer, String comments)

Properties properties = new Properties();
properties.setProperty("key1", "value1");
properties.setProperty("key2", "value2");

try (Writer writer = new FileWriter("output.properties")) {
    // 將屬性列表寫入輸出字符流
    properties.store(writer, "Comments for the output file");
} catch (IOException e) {
    e.printStackTrace();
}

3. Demo

上述的API方法可適當(dāng)選擇,完整的Demo可充分了解這個類的整體邏輯:

import java.io.*;
import java.util.Properties;

public class PropertiesDemo {

    public static void main(String[] args) {
        // 創(chuàng)建 Properties 對象
        Properties properties = new Properties();

        // 設(shè)置屬性值
        properties.setProperty("username", "碼農(nóng)研究僧");
        properties.setProperty("password", "123456789");
        properties.setProperty("server", "https://blog.csdn.net/weixin_47872288");

        // 將屬性列表寫入輸出流
        try (OutputStream output = new FileOutputStream("config.properties")) {
            properties.store(output, "Sample Configuration");
            System.out.println("Properties written to config.properties");
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 從輸入流中讀取屬性列表
        try (InputStream input = new FileInputStream("config.properties")) {
            properties.load(input);

            // 獲取屬性值
            String username = properties.getProperty("username");
            String password = properties.getProperty("password");
            String server = properties.getProperty("server");

            System.out.println("Username: " + username);
            System.out.println("Password: " + password);
            System.out.println("Server: " + server);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 打印屬性列表到控制臺
        /**
         * -- listing properties --
         * password=123456789
         * server=https://blog.csdn.net/weixin_47872288
         * username=碼農(nóng)研究僧
         */
        properties.list(System.out);
    }
}

終端輸出結(jié)果如下:

在這里插入圖片描述

到此這篇關(guān)于深入理解Java中的Properties類的文章就介紹到這了,更多相關(guān)Java Properties類內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

  • javaWeb自定義標簽用法實例詳解

    javaWeb自定義標簽用法實例詳解

    這篇文章主要介紹了javaWeb自定義標簽用法,結(jié)合實例形式分析了javaweb自定義標簽的功能、定義方法及執(zhí)行原理,需要的朋友可以參考下
    2017-04-04
  • Maven安裝與配置圖文教程

    Maven安裝與配置圖文教程

    這篇文章主要為大家詳細介紹了Maven安裝與配置圖文教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-10-10
  • 一篇帶你入門Java垃圾回收器

    一篇帶你入門Java垃圾回收器

    垃圾收集器是垃圾回收算法(標記-清除算法、復(fù)制算法、標記-整理算法、火車算法)的具體實現(xiàn),不同商家、不同版本的JVM所提供的垃圾收集器可能會有很在差別
    2021-06-06
  • JAVA不可變類(immutable)機制與String的不可變性(推薦)

    JAVA不可變類(immutable)機制與String的不可變性(推薦)

    這篇文章主要介紹了JAVA不可變類(immutable)機制與String的不可變性(推薦)的相關(guān)資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-08-08
  • Java圖像處理之RGB調(diào)色面板

    Java圖像處理之RGB調(diào)色面板

    這篇文章主要為大家詳細介紹了Java圖像處理之RGB調(diào)色面板,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • Java源碼解析TreeMap簡介

    Java源碼解析TreeMap簡介

    今天小編就為大家分享一篇關(guān)于Java源碼解析TreeMap簡介,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • java構(gòu)造http請求的幾種方式(附源碼)

    java構(gòu)造http請求的幾種方式(附源碼)

    本文主要介紹了java構(gòu)造http請求的幾種方式,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • java實現(xiàn)日歷功能

    java實現(xiàn)日歷功能

    這篇文章主要為大家詳細介紹了java實現(xiàn)日歷功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • Spring的初始化和XML解析的實現(xiàn)

    Spring的初始化和XML解析的實現(xiàn)

    這篇文章主要介紹了Spring的初始化和XML解析的實現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-03-03
  • Java多線程Runable售票系統(tǒng)實現(xiàn)過程解析

    Java多線程Runable售票系統(tǒng)實現(xiàn)過程解析

    這篇文章主要介紹了Java多線程Runable售票系統(tǒng)實現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-06-06

最新評論