Java動態(tài)替換properties文件中鍵值方式
Java動態(tài)替換properties文件中鍵值
最近遇到需要動態(tài)替換json文件中的值的需求,類比在 Java 中讀取 properties
文件,并根據(jù)傳入的多個參數(shù)替換相應(yīng)的 key 的屬性值,基于使用 java.util.Properties
類來實現(xiàn)。
實現(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"; // 要修改的多個 key 及其新值 Map<String, String> keyValues = Map.of( "key1", "newValue1", "key2", "newValue2" ); try { // 調(diào)用方法來修改 properties 文件中的多個 key 的值 modifyProperties(propertiesFilePath, keyValues); } catch (IOException e) { e.printStackTrace(); } } /** * 修改 properties 文件中指定的多個 key 的值 * * @param propertiesFilePath properties 文件路徑 * @param keyValues 要修改的 key 及其新值的 Map * @throws IOException 如果文件讀取或?qū)懭霑r出現(xiàn)錯誤 */ public static void modifyProperties(String propertiesFilePath, Map<String, String> keyValues) throws IOException { // 創(chuàng)建 Properties 對象,用于處理 properties 文件 Properties properties = new Properties(); // 讀取 properties 文件 try (InputStream input = new FileInputStream(propertiesFilePath)) { properties.load(input); } // 遍歷要修改的 key 值,并更新到 Properties 對象中 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 對象寫回到文件中 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é)
通過 Properties 類讀取 properties 文件,并根據(jù)傳入的 Map 動態(tài)地替換多個鍵值對。
該方法靈活且高效,適用于需要對配置文件進行動態(tài)修改的場景。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
maven如何動態(tài)統(tǒng)一修改版本號的方法步驟
這篇文章主要介紹了maven如何動態(tài)統(tǒng)一修改版本號的方法步驟,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12java使用poi讀取ppt文件和poi讀取excel、word示例
這篇文章主要介紹了java使用poi讀取ppt文件和poi讀取excel、word示例,需要的朋友可以參考下2014-03-03mybatis-plus實體類主鍵策略有3種(小結(jié))
這篇文章主要介紹了mybatis-plus實體類主鍵策略有3種(小結(jié)),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08Java?SimpleDateFormat與System類使用示例詳解
這篇文章主要介紹了Java?SimpleDateFormat與System類使用示例,對于SimpleDateFormat類,是一個用來區(qū)分區(qū)域設(shè)置的方式進行日期的是指,以及對日期進行處理分析的一個實現(xiàn)類2022-11-11關(guān)于Java中BeanMap進行對象與Map的相互轉(zhuǎn)換問題
這篇文章主要介紹了利用BeanMap進行對象與Map的相互轉(zhuǎn)換,通過net.sf.cglib.beans.BeanMap類中的方法來轉(zhuǎn)換,效率極高,本文給大家分享實現(xiàn)代碼,感興趣的朋友一起看看吧2022-03-03Spring boot @RequestBody數(shù)據(jù)傳遞過程詳解
這篇文章主要介紹了Spring boot @RequestBody數(shù)據(jù)傳遞過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-12-12