将文件夹下的所有文件替换内容和替换扩展名

将文件夹下的所有文件替换内容和替换扩展名,原文章中使用的gb2312,这里改为了utf-8

package com.tianwen.eco.util;

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.InputStreamReader;

import java.io.PrintWriter;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: xiaozan
 * @Date: 2021/03/27/9:25
 * @Description:
 */
public class Test {

    /**

     * @author itmyhome

     */

    public static void main(String[] args) {
        File f = new File("C:/ESClientRHL_bak");

        print(f, 0);

    }

    /**

     * 遍历目录

     *

     * @param f

     * @param len

     */

    public static void print(File f, int len) {
        File[] file = f.listFiles();

        for (int i = 0; i < file.length; i++) {
            if (file[i].isDirectory()) { //推断是否目录

                print(file[i], len + 1);

            }

// 为防止输出文件覆盖源文件,所以更改输出盘路径 也可自行设置其它路径

            File outPath = new File(file[i].getParent().replace("C:", "E:"));

            File readfile = new File(file[i].getAbsolutePath());

            if (!readfile.isDirectory()) {
                String filename = readfile.getName(); // 读到的文件名称

                String absolutepath = readfile.getAbsolutePath(); // 文件的绝对路径

                readFile(absolutepath, filename, i, outPath); // 调用 readFile

            }

        }

    }

    /**

     * 读取目录下的文件

     *

     * @return

     */

    public static void readFile(String absolutepath, String filename,

                                int index, File outPath) {
        if(filename.contains(".md"))
        {
            filename= filename.replaceAll(".md",".txt");
        }
        try {
            BufferedReader bufReader = new BufferedReader(new InputStreamReader(

                    new FileInputStream(absolutepath), "utf-8")); // 数据流读取文件

            StringBuffer strBuffer = new StringBuffer();

            String newStr = "替换成新内容";

            String oldStr = "原本内容";

            for (String temp = null; (temp = bufReader.readLine()) != null;temp = null)
            {

                if ((temp.indexOf(oldStr) != -1) &&

                        (temp.indexOf(oldStr) != -1)) { // 推断当前行是否存在想要替换掉的字符

                    temp = temp.replace(oldStr, newStr); // 此处进行替换

                }
                
                strBuffer.append(temp);

                strBuffer.append(System.getProperty("line.separator")); // 换行符

            }

            bufReader.close();

            if (outPath.exists() == false) { // 检查输出目录是否存在,若不存在先创建

                outPath.mkdirs();

                System.out.println("已成功创建输出目录:" + outPath);

            }

            PrintWriter printWriter = new PrintWriter(outPath + "/" +

                    filename, "utf-8"); // 替换后输出文件路径

            printWriter.write(strBuffer.toString().toCharArray()); //又一次写入

            printWriter.flush();

            printWriter.close();

            System.out.println("第 " + (index + 1) + " 个文件 " + absolutepath +

                    " 已成功输出到 " + outPath + "/" + filename);

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

        }

    }

}

https://blog.csdn.net/weixin_30479433/article/details/114096044

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值