code: public static void changeProperty(String filename, Map<String, Object> keyValuePairs) throws IOException {
          if (keyValuePairs.size() == 0) {
              return;
          }
  
          final File file = new File(filename);
          final File tmpFile = new File(file + ".tmp");
  
          PrintWriter pw = new PrintWriter(tmpFile);
          BufferedReader br = new BufferedReader(new FileReader(file));
  
          Set<String> keys = keyValuePairs.keySet();
          List<String> inConfig = new ArrayList<>();
  
          for (String line; (line = br.readLine()) != null; ) {
              int keyPos = line.indexOf('=');
              if (keyPos > -1) {
                  String key = line.substring(0, keyPos).trim();
  
                  if (!key.trim().startsWith("#") && keys.contains(key) && !inConfig.contains(key)) {
                      // Check if the line has any comments.  Split by '#' to funs all tokens.
                      String[] keyValuePair = line.split("=");
  
                      StringBuilder lineBuilder = new StringBuilder();
                      lineBuilder.append(keyValuePair[0].trim()).append(" = ").append(keyValuePairs.get(key));
  
                      line = lineBuilder.toString();
                      inConfig.add(key);
                  }
              }
              pw.println(line);
          }
  
          for (String key : keys) {
              if (!inConfig.contains(key)) {
                  StringBuilder lineBuilder = new StringBuilder();
                  lineBuilder.append(key).append(" = ").append(keyValuePairs.get(key));
                  pw.println(lineBuilder.toString());
              }
          }
  
          br.close();
          pw.close();
          file.delete();
          tmpFile.renameTo(file);
      }
docs: Method to modify the Postgres config postgresql.conf based on key-value pairs
  
  @param filename      The filename of the config to be updated.
  @param keyValuePairs A map of key-value pairs.