栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

文章目录

一、需求描述二、具体实现总结

一、需求描述

​ 本人经常写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;

public class DemoApplication {

    
    public static List listPath = new ArrayList();

    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() {
            @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();
                }

            }
        });
    }


    
    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 {
        }
    }
}
总结

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/781256.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号