package com.education.common.utils;


import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class StringToTxt {
    public static void main(String[] args) {
        String content = "这是要写入文件的字符串内容。";
        String filePath = "d:/output.txt";
        writeStringToFile(content, filePath);
    }

    public static void writeStringToFile(String content, String filePath) {
        try (FileWriter writer = new FileWriter(filePath)) {
            writer.write(content);
            System.out.println("文件写入成功！");
        } catch (IOException e) {
            System.out.println("文件写入时发生错误：" + e.getMessage());
        }
    }

    /*public static void main(String[] args) {
        String input = "Hello, ${a}! Your ${b} is ready.";
        Map<String, String> replacements = new HashMap<>();
        replacements.put("a", "John");
        replacements.put("b", "order");

        String output = replacePlaceholders(input, replacements);
        System.out.println(output);
    }*/

    public static String replacePlaceholders(String input, Map<String, String> replacements) {
        String result = input;
        for (Map.Entry<String, String> entry : replacements.entrySet()) {
            String placeholder = "${" + entry.getKey() + "}";
            result = result.replace(placeholder, entry.getValue());
        }
        return result;
    }
}