package replaceTxtInFolders;

import java.io.*;
import java.nio.file.Paths;
import java.util.LinkedHashMap;
import java.util.Map;

public class ReadFilesAndReplaceText {

    // 需要被替换的文件所在的文件夹路径(绝对路径)
    static String fileFolder = ".../files";
    // 替换文字map(替换前文字,替换后文字)文件的路径(绝对路径)
    static String mapPath = ".../mapFiles.txt";
    static Boolean onlyPrint = true;   // true:不将结果写入到文件,只打印到控制台; false: 写入进文件中;
    private static final Map<String, String> replaceMap = new LinkedHashMap<>();

    static {
        try {
            loadMapFromFile(mapPath);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
  
    public static void main(String[] args) {

        // 批量替换文件夹中的文件内容
        replaceFileContent(fileFolder);
    }

    static void replaceFileContent(String folderPath) {
        File folder = new File(folderPath);

        if (folder.exists() && folder.isDirectory()) {
            File[] files = folder.listFiles();
            if (files != null) {
                for (File file : files) {
//                    if (file.isFile() && file.getName().endsWith(".java")) {
                    if (file.isFile()) {
                        String filePath = Paths.get(folderPath,file.getName()).toString();

                        StringBuilder updatedContentBuilder = new StringBuilder();

                        // 读取文件内容
                        try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "UTF-8"))) {
                            String line;
                            while ((line = reader.readLine()) != null) {
                                // 遍历 LinkedHashMap 并按照插入顺序输出键值对
                                for (Map.Entry<String, String> entry : replaceMap.entrySet()) {
//                                    System.out.println(entry.getKey() + ": " + entry.getValue());
                                    String replaceFrom = entry.getKey();
                                    String replaceTo = entry.getValue();
                                    line = line.replace(replaceFrom, replaceTo);
                                }
                                updatedContentBuilder.append(line).append("\n");
                            }
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                        System.out.println(updatedContentBuilder.toString());
                        // 写入,设置编码格式
                        if (!onlyPrint) {
                            try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath), "UTF-8"))) { //覆盖原来的文件内容
                                writer.write(updatedContentBuilder.toString());
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }

                    }
                }
            }
        }
    }

    public static String readTextFileToString(String filePath, String encoding) {
        StringBuilder contentBuilder = new StringBuilder();
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filePath)), encoding))) {
            String line;
            while ((line = reader.readLine()) != null) {
                contentBuilder.append(line).append("\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return contentBuilder.toString();
    }


    private static void loadMapFromFile(String filePath) throws IOException {
        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = reader.readLine()) != null) {
                String[] keyValue = line.split(",");
                if (keyValue.length == 2) {
                    String key = keyValue[0].trim();
                    String value = keyValue[1].trim();
                    replaceMap.put(key, value);
                }
            }
        }
    }
}