替換文件中的特定字符串--Java實現(xiàn)

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 需要替換的內(nèi)容
         * arg2 新內(nèi)容
         */
        replaceTxtByStr("D:\\test\\man.json", "$file$", "test_000");
        iteratorDirectory("D:\\test", "$file$", "test_000");
    }

    /**
     * 遍歷文件夾下的所有文件進(jìn)行文本替換
     *
     * @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());
        }
    }

}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容