替换文件中的特定字符串--Java实现

2020-07-28  本文已影响0人  cherishpf
package utils;

import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

/**
 * @author YPF
 */
@Slf4j
public class ReplaceWordUtil {
    public static void main(String[] args) {
        /**
         * arg0 文件路径
         * arg1 需要替换的内容
         * arg2 新内容
         */
        replaceTxtByStr("D:\\test\\man.json", "$file$", "test_000");
        iteratorDirectory("D:\\test", "$file$", "test_000");
    }

    /**
     * 遍历文件夹下的所有文件进行文本替换
     *
     * @param fileDir
     * @param oldStr
     * @param replaceStr
     */
    public static void iteratorDirectory(String fileDir, String oldStr, String replaceStr) {
        File file = new File(fileDir);
        if (file.isDirectory()) {
            String[] fileList = file.list();
            for (int i = 0; i < fileList.length; i++) {
                iteratorDirectory(fileDir + File.separator + fileList[i], oldStr, replaceStr);
            }
        } else {
            replaceTxtByStr(fileDir, oldStr, replaceStr);
        }
    }

    /**
     * 替换文件中的字符串
     *
     * @param filePath
     * @param oldStr
     * @param replaceStr
     */
    public static void replaceTxtByStr(String filePath, String oldStr, String replaceStr) {
        String temp = "";
        int len = oldStr.length();
        StringBuffer tempBuf = new StringBuffer();
        try {
            File file = new File(filePath);
            FileInputStream fis = new FileInputStream(file);
            InputStreamReader isr = new InputStreamReader(fis);
            BufferedReader br = new BufferedReader(isr);
            StringBuffer buf = new StringBuffer();


            // 按行读取,仅替换每行第一个匹配的字符串
            //while ((temp = br.readLine()) != null) {
            //    if (temp.contains(oldStr)) {
            //        int index = temp.indexOf(oldStr);
            //        tempBuf.append(temp);
            //        tempBuf.replace(index, index + len, replaceStr);
            //        buf.append(tempBuf);
            //        tempBuf.setLength(0);
            //    } else {
            //        buf.append(temp);
            //    }
            //    buf = buf.append(System.getProperty("line.separator"));
            // }

           // 替换所有匹配的字符串
           for (String temp = null; (temp = br.readLine()) != null; temp = null) {
                if (temp.indexOf(oldStr) != -1) {
                    temp = temp.replace(oldStr, replaceStr);
                }
                buf.append(temp);
                buf.append(System.getProperty("line.separator"));
            }

            br.close();
            FileOutputStream fos = new FileOutputStream(file);
            PrintWriter pw = new PrintWriter(fos);
            pw.write(buf.toString().toCharArray());
            pw.flush();
            pw.close();
        } catch (IOException e) {
            e.printStackTrace();
            log.error("修改文件{}失败,错误信息: {}", filePath, e.getMessage());
        }
    }

}
上一篇 下一篇

猜你喜欢

热点阅读