SoFunction
Updated on 2025-03-08

Java dynamically replaces key-value methods in properties files

Java dynamically replaces key values ​​in properties files

Recently, I encountered the need to dynamically replace the values ​​in json files, and the analogy is read in Javapropertiesfile, and replace the attribute value of the corresponding key according to the multiple parameters passed in, based on the useclass to implement.

accomplish

import ;
import ;
import ;
import ;
import ;
import ;
import ;

public class PropertiesModifier {

    public static void main(String[] args) {
        // Properties file path        String propertiesFilePath = "xxxx_file.properties"; 

        // Multiple keys to be modified and their new values        Map<String, String> keyValues = (
                "key1", "newValue1", 
                "key2", "newValue2"  
        );

        try {
            // Call method to modify the values ​​of multiple keys in the properties file            modifyProperties(propertiesFilePath, keyValues);
        } catch (IOException e) {
            ();
        }
    }

    /**
      * Modify the values ​​of multiple keys specified in the properties file
      *
      * @param propertiesFilePath properties file path
      * @param keyValues ​​The key to be modified and its new value Map
      * @throws IOException If an error occurs while reading or writing a file
      */
    public static void modifyProperties(String propertiesFilePath, Map<String, String> keyValues) throws IOException {
        // Create Properties object to process properties files        Properties properties = new Properties();

        // Read properties file        try (InputStream input = new FileInputStream(propertiesFilePath)) {
            (input);
        }

        // traverse the key value to be modified and update it to the Properties object        for (<String, String> entry : ()) {
            String key = ();
            String newValue = ();

            // If key exists, replace its value            if ((key)) {
                (key, newValue);
                ("Updated key: " + key + ", New Value: " + newValue);
            } else {
                ("Key not found: " + key);
            }
        }

        // Write the modified Properties object back to the file        try (OutputStream output = new FileOutputStream(propertiesFilePath)) {
            (output, "Updated properties file");
        }

        // Output the modified properties content        ("Modified Properties Content: " + properties);
    }
}

Effect

  • Before modifying properties file
=jdbc:mysql://localhost:3306/mydb
=root
=secret
=MyApplication
  • Data to be modified
Map<String, String> keyValues = (
        "", "admin",
        "", "ICQQ"
);
  • Modified properties file
=jdbc:mysql://localhost:3306/mydb
=admin
=secret
=ICQQ

Summarize

Read the properties file through the Properties class and dynamically replace multiple key-value pairs according to the incoming Map.

This method is flexible and efficient, and is suitable for scenarios where configuration files need to be modified dynamically.

The above is personal experience. I hope you can give you a reference and I hope you can support me more.