import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;
public class ConfigWriter {
/**
* Writes configuration data to a text file.
* @param filename The name of the file to write to.
* @param configData A map containing the configuration parameters.
*/
public static void writeConfig(String filename, Map<String, String> configData) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(filename))) {
// Write each key-value pair to the file
for (Map.Entry<String, String> entry : configData.entrySet()) {
writer.write(entry.getKey() + "=" + entry.getValue());
writer.newLine(); // Add a newline after each entry
}
} catch (IOException e) {
System.err.println("Error writing to file: " + e.getMessage());
}
}
public static void main(String[] args) {
//Example Usage
Map<String, String> config = Map.of(
"logLevel", "INFO",
"host", "localhost",
"port", "8080"
);
writeConfig("config.txt", config);
}
}
Add your comment