Java实现批量替换文本文档中内容

Java实现批量替换文本文档中内容
有更好的方案大佬们也可以在评论区指出

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class FileUtil {
    //存储要修改的内容
    private final Map<String, String> map;

    public FileUtil(Map<String, String> map) {
        this.map = map;
    }

    public static void main(String[] args) {

        //读取指定文件夹下的所有文件
        //给我你的目录文件夹路径
        String filepath = "F:\\A";
        File file = new File(filepath);
        Map<String, String> map = new HashMap<>();
        map.put("要修改的内容1", "修改的内容1");
        map.put("要修改的内容2;", "修改的内容2");
        new FileUtil(map).refreshFileList(file);
        
    }

    public void refreshFileList(String filepath) {
        refreshFileList(new File(filepath));
    }

    public void refreshFileList(File filepath) {
        if (!filepath.exists()) {
            System.out.println("此路径不存在");
            return;
        }
        File[] fileList = filepath.listFiles();
        if (fileList == null || fileList.length < 1) {
            System.out.println("空文件夹");
            return;
        }
        for (File file : fileList) {
            if (file.isDirectory()) {
                //如果是文件夹递归扫描
                refreshFileList(file);
            } else {
                String filename = file.getName();//读到的文件名
                String strFileName = file.getAbsolutePath();//文件路径
                //截取文件格式
                String SufName = filename.substring(filename.lastIndexOf(".") + 1);
                //排除不需要扫描的文件
//                if (SufName.equals("rar") || SufName.equals("jpg") || SufName.equals("png") || SufName.equals("jar") || SufName.equals("doc") || SufName.equals("xls") || SufName.equals("gif") || SufName.equals("wmz")) {
//                    continue;
//                }
                //或者指定扫描文件
                if (SufName.equalsIgnoreCase("html") || SufName.equalsIgnoreCase("java")) {
                    changeFile(file);
                }
            }
        }
    }

    /**
     * 修改文件
     *
     * @param file
     */
    private void changeFile(File file) {
        String s = null;
        try (FileInputStream fis = new FileInputStream(file);
             BufferedReader reader = new BufferedReader(new InputStreamReader(fis));) {

            //之所以用BufferedReader,而不是直接用BufferedInputStream读取,是因为BufferedInputStream是InputStream的间接子类,
            //InputStream的read方法读取的是一个byte,而一个中文占两个byte,所以可能会出现读到半个汉字的情况,就是乱码.
            //BufferedReader继承自Reader,该类的read方法读取的是char,所以无论如何不会出现读个半个汉字的.
            StringBuilder result = new StringBuilder();
            while (reader.ready()) {
                result.append((char) reader.read());
            }
            s = result.toString();
            Set<Map.Entry<String, String>> entries = map.entrySet();
            for (Map.Entry<String, String> mapKey : entries) {
                if (s.contains(mapKey.getKey())) { //判断当前行是否存在想要替换掉的字符 
                    s = s.replace(mapKey.getKey(), mapKey.getValue());//替换为你想替换的内容
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        writerFile(s, file);

    }

    /**
     * 写入文件
     *
     * @param s
     * @param file
     */
    public void writerFile(String s, File file) {
        try (FileOutputStream fos = new FileOutputStream(file);
             BufferedOutputStream bos = new BufferedOutputStream(fos)) {
            bos.write(s.getBytes());
            System.out.println("文件修改成功!");
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("文件修改失败!");
        }
    }

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值