Java動(dòng)態(tài)替換properties文件中鍵值方式
Java動(dòng)態(tài)替換properties文件中鍵值
最近遇到需要?jiǎng)討B(tài)替換json文件中的值的需求,類(lèi)比在 Java 中讀取 properties
文件,并根據(jù)傳入的多個(gè)參數(shù)替換相應(yīng)的 key 的屬性值,基于使用 java.util.Properties
類(lèi)來(lái)實(shí)現(xiàn)。
實(shí)現(xiàn)
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.Map; import java.util.Properties; public class PropertiesModifier { public static void main(String[] args) { // Properties 文件路徑 String propertiesFilePath = "xxxx_file.properties"; // 要修改的多個(gè) key 及其新值 Map<String, String> keyValues = Map.of( "key1", "newValue1", "key2", "newValue2" ); try { // 調(diào)用方法來(lái)修改 properties 文件中的多個(gè) key 的值 modifyProperties(propertiesFilePath, keyValues); } catch (IOException e) { e.printStackTrace(); } } /** * 修改 properties 文件中指定的多個(gè) key 的值 * * @param propertiesFilePath properties 文件路徑 * @param keyValues 要修改的 key 及其新值的 Map * @throws IOException 如果文件讀取或?qū)懭霑r(shí)出現(xiàn)錯(cuò)誤 */ public static void modifyProperties(String propertiesFilePath, Map<String, String> keyValues) throws IOException { // 創(chuàng)建 Properties 對(duì)象,用于處理 properties 文件 Properties properties = new Properties(); // 讀取 properties 文件 try (InputStream input = new FileInputStream(propertiesFilePath)) { properties.load(input); } // 遍歷要修改的 key 值,并更新到 Properties 對(duì)象中 for (Map.Entry<String, String> entry : keyValues.entrySet()) { String key = entry.getKey(); String newValue = entry.getValue(); // 如果 key 存在,則替換其值 if (properties.containsKey(key)) { properties.setProperty(key, newValue); System.out.println("Updated key: " + key + ", New Value: " + newValue); } else { System.out.println("Key not found: " + key); } } // 將修改后的 Properties 對(duì)象寫(xiě)回到文件中 try (OutputStream output = new FileOutputStream(propertiesFilePath)) { properties.store(output, "Updated properties file"); } // 輸出修改后的 properties 內(nèi)容 System.out.println("Modified Properties Content: " + properties); } }
效果
- 修改前 properties 文件
database.url=jdbc:mysql://localhost:3306/mydb database.username=root database.password=secret app.name=MyApplication
- 待修改數(shù)據(jù)
Map<String, String> keyValues = Map.of( "database.username", "admin", "app.name", "ICQQ" );
- 修改后 properties 文件
database.url=jdbc:mysql://localhost:3306/mydb database.username=admin database.password=secret app.name=ICQQ
總結(jié)
通過(guò) Properties 類(lèi)讀取 properties 文件,并根據(jù)傳入的 Map 動(dòng)態(tài)地替換多個(gè)鍵值對(duì)。
該方法靈活且高效,適用于需要對(duì)配置文件進(jìn)行動(dòng)態(tài)修改的場(chǎng)景。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
解決SpringSecurity 一直登錄失敗的問(wèn)題
這篇文章主要介紹了解決SpringSecurity 一直登錄失敗的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06Java編程使用UDP建立群聊系統(tǒng)代碼實(shí)例
這篇文章主要介紹了Java編程使用UDP建立群聊系統(tǒng)代碼實(shí)例,具有一定借鑒價(jià)值,需要的朋友可以參考下。2018-01-01maven如何動(dòng)態(tài)統(tǒng)一修改版本號(hào)的方法步驟
這篇文章主要介紹了maven如何動(dòng)態(tài)統(tǒng)一修改版本號(hào)的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12Java中MyBatis的動(dòng)態(tài)語(yǔ)句詳解
這篇文章主要介紹了Java中MyBatis的動(dòng)態(tài)語(yǔ)句詳解,動(dòng)態(tài) SQL 是 MyBatis 的強(qiáng)大特性之一,通過(guò)不同參數(shù)生成不同的 SQL,可以動(dòng)態(tài)地對(duì)數(shù)據(jù)持久層進(jìn)行操作,而不需要每個(gè)數(shù)據(jù)訪(fǎng)問(wèn)操作都要進(jìn)行手動(dòng)地拼接 SQL 語(yǔ)句,需要的朋友可以參考下2023-08-08java使用poi讀取ppt文件和poi讀取excel、word示例
這篇文章主要介紹了java使用poi讀取ppt文件和poi讀取excel、word示例,需要的朋友可以參考下2014-03-03mybatis-plus實(shí)體類(lèi)主鍵策略有3種(小結(jié))
這篇文章主要介紹了mybatis-plus實(shí)體類(lèi)主鍵策略有3種(小結(jié)),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08Java?SimpleDateFormat與System類(lèi)使用示例詳解
這篇文章主要介紹了Java?SimpleDateFormat與System類(lèi)使用示例,對(duì)于SimpleDateFormat類(lèi),是一個(gè)用來(lái)區(qū)分區(qū)域設(shè)置的方式進(jìn)行日期的是指,以及對(duì)日期進(jìn)行處理分析的一個(gè)實(shí)現(xiàn)類(lèi)2022-11-11關(guān)于Java中BeanMap進(jìn)行對(duì)象與Map的相互轉(zhuǎn)換問(wèn)題
這篇文章主要介紹了利用BeanMap進(jìn)行對(duì)象與Map的相互轉(zhuǎn)換,通過(guò)net.sf.cglib.beans.BeanMap類(lèi)中的方法來(lái)轉(zhuǎn)換,效率極高,本文給大家分享實(shí)現(xiàn)代碼,感興趣的朋友一起看看吧2022-03-03Spring boot @RequestBody數(shù)據(jù)傳遞過(guò)程詳解
這篇文章主要介紹了Spring boot @RequestBody數(shù)據(jù)傳遞過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12