1. import java.io.BufferedWriter;
  2. import java.io.FileWriter;
  3. import java.io.IOException;
  4. import java.util.Map;
  5. public class ConfigWriter {
  6. /**
  7. * Writes configuration data to a text file.
  8. * @param filename The name of the file to write to.
  9. * @param configData A map containing the configuration parameters.
  10. */
  11. public static void writeConfig(String filename, Map<String, String> configData) {
  12. try (BufferedWriter writer = new BufferedWriter(new FileWriter(filename))) {
  13. // Write each key-value pair to the file
  14. for (Map.Entry<String, String> entry : configData.entrySet()) {
  15. writer.write(entry.getKey() + "=" + entry.getValue());
  16. writer.newLine(); // Add a newline after each entry
  17. }
  18. } catch (IOException e) {
  19. System.err.println("Error writing to file: " + e.getMessage());
  20. }
  21. }
  22. public static void main(String[] args) {
  23. //Example Usage
  24. Map<String, String> config = Map.of(
  25. "logLevel", "INFO",
  26. "host", "localhost",
  27. "port", "8080"
  28. );
  29. writeConfig("config.txt", config);
  30. }
  31. }

Add your comment