0
点赞
收藏
分享

微信扫一扫

Java替换目录下的所有文本文件的某一行内容

素的盐 2022-03-26 阅读 56

文章目录

一、需求描述

​ 本人经常写CSDN文章,上面的图片链接来自gitee图床,突然有一天图床用不了了,我需要切换到腾讯云COS上,那此时我目录下这么多的文本文件需要更换图片链接,我要是挨个文件去替换,我这几十个文件换下来我得崩溃啊,所以我就搞了个这个代码。

二、具体实现

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
/**
 * @author lijiamin
 */
public class DemoApplication {

    /** xlsx文件路径 */
    public static List<String> listPath = new ArrayList<String>();

    public static final String oldPath = "https://gitee.com/xxxxxxxx/drawing-bed/xxxxxx/master/";
    public static final String newPath = "https://csdn-pic-xxxxxxxx.cos.ap-guangzhou.myqcloud.com/";

    public static void main(String[] args) throws FileNotFoundException {
        // 文件路径
        String path_dir = "D:\\1_云端笔记薄\\test";
        // 查询所有md后缀的文件
        folderMethod2(path_dir);
        System.out.println("目前listPath集合有数据量:" + listPath.size());
        // 读取文件内容并替换
        amend();
    }

    /**
     * 读取文件内容并替换
     */
    public static void amend() {
        listPath.forEach(new Consumer<String>() {
            @Override
            public void accept(String strPath) {
                try {
                    File file = new File(strPath);
                    Long fileLength = file.length();
                    byte[] fileContext = new byte[fileLength.intValue()];
                    // 输入流
                    FileInputStream in = new FileInputStream(strPath);
                    in.read(fileContext);
                    in.close();
                    String str = new String(fileContext);
                    String replaceAll = str.replaceAll(oldPath, newPath);
                    // 输出流
                    PrintWriter out = new PrintWriter(strPath);
                    out.write(replaceAll.toCharArray());
                    out.flush();
                    out.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });
    }


    /**
     * 查找所有md后缀的文件
     * @param path
     */
    public static void folderMethod2(String path) {
        File file = new File(path);
        if (file.exists()) {
            File[] files = file.listFiles();
            if (null != files) {
                for (File file2 : files) {
                    if (file2.isDirectory()) {
                        folderMethod2(file2.getAbsolutePath());
                    } else {
                        String file2AbsolutePath = file2.getAbsolutePath();
                        int length = file2AbsolutePath.length();
                        if (file2AbsolutePath.substring(file2AbsolutePath.lastIndexOf("."), length).equals(".md")) {
                            listPath.add(file2AbsolutePath);
                        }
                    }
                }
            }
        } else {
        }
    }
}

总结

提示:这里对文章进行总结:
例如:以上就是今天要讲的内容。

举报

相关推荐

0 条评论