文章目录
- 背景
- 使用方式
- 代码
BatchReplcae-MDhref:批量替换指定目录下md文件中的图片链接
背景
前两天Gitee的图床都被封了,所有的外链都无法访问,笔记全体火葬场,就像下面这样:
然后立马就将Gitee图床里的图片全部转移到了阿里云OSS存储服务里去了。
此时我的所有笔记想要替换其中的图片链接可是个大工程。
此时需求就来了,将目录下所有文件内容中Gitee链接替换为阿里云的链接。
思路如下:
1、核心功能三步骤:读取文件内容;将读取到的内容所有链接进行替换;将替换好的内容写入到目标路径。(单位是文件)
2、实现遍历文件目录:使用Files.walk()整个函数来进行。
项目地址:BatchReplcae-MDhref
使用方式
说明:本项目仅仅使用的是JDK提供的类库,无需引入第三方类库。
1、填写四个配置项
源目录:指的是就是你要替换的目录文件夹路径。
目标目录:就是所有文件替换好之后输出的路径。
我的需求是将我的笔记目录中所有md里内容替换如下:
- https://gitee.com/changluJava/picture-bed/raw/master/替换为https://pictured-bed.oss-cn-beijing.aliyuncs.com/img/beifen-gitee/
所以我的完整配置是:
//源目录
private static String COPY_ORIGN = "C:\\Users\\93997\\Desktop\\test";
//目标目录(可以填写自己本身源路径,达到覆盖效果。建议是输出到其他路径,防止有问题)
private static String COPY_TO = "C:\\Users\\93997\\Desktop\\test";
//被替换内容
private static String tihuan = "https://gitee.com/changluJava/picture-bed/raw/master/";
//替换内容
private static String tihuanTO = "https://pictured-bed.oss-cn-beijing.aliyuncs.com/img/beifen-gitee/";
2、运行代码即可:
OK,此时打开文件可以看到里面的原来图床地址已经替换如下:
代码
代码地址见:BatchReplcae-MDhref,好用记得点个Star噢,谢谢!
package com.changlu;
import java.io.*;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.logging.Logger;
/**
* 效果:将源目录所有md文件内容中tihuan配置内容替换为tihuanTO内容,最终输出到指定路径。
* 配置说明:填写四个配置即可
* COPY_ORIGN:源目录
* COPY_ORIGN:目标输出目录(可填自己源目录)
* tihuan:被替换内容
* tihuanTO:替换内容
*
* 说明:想要将md文件中的1替换为2,那么被替换内容就是1、替换内容就是2
*
* @ClassName updateMD
* @Author ChangLu
* @Date 3/26/2022 11:42 AM
* @Description 批量替换MD图片路径工具
*/
public class BatchReplaceUtil {
private static Logger log = Logger.getLogger("com.changlu.updateMD");
//源目录
private static String COPY_ORIGN = "C:\\Users\\93997\\Desktop\\test";
//目标目录
private static String COPY_TO = "C:\\Users\\93997\\Desktop\\test";
//被替换内容
private static String tihuan = "https://gitee.com/changluJava/picture-bed/raw/master/";
//替换内容
private static String tihuanTO = "https://pictured-bed.oss-cn-beijing.aliyuncs.com/img/beifen-gitee/";
public static void main(String[] args) throws Exception{
Files.walk(Paths.get(COPY_ORIGN)).forEach(path->{
String originFile = path.toFile().getAbsolutePath();
String targetFile = path.toFile().getAbsolutePath().replace(COPY_ORIGN, COPY_TO);
try {
//若是文件目录,则创建目标目录
if (Files.isDirectory(path)) {
//判断是否存在,不存在则创建
if (!new File(targetFile).exists()) {
Files.createDirectory(Paths.get(targetFile));
}
}else{
if (targetFile.contains(".md")){
//执行替换任务
doWork(originFile, targetFile, tihuan, tihuanTO);
System.out.println(targetFile + "已经覆盖完成!");
}else{
Files.copy(path, Paths.get(targetFile));
}
}
}catch (Exception e){
if (e instanceof FileAlreadyExistsException){
log.info("目标文件:" + targetFile + " 已经存在");
}else{
e.printStackTrace();
}
}
});
System.out.println("所有的文件已全部替换!");
}
private static void doWork(String originFile,String targetFile,String tihuan,String tihuanTO) throws IOException {
//读取一个文件,获取到内容
String content = readFile(originFile);
//将内容中的文字进行替换
String newContent = content.replaceAll(tihuan, tihuanTO);
//写入到指定的目录
writeFile(newContent, targetFile);
}
private static void writeFile(String newContent,String targetFile) throws IOException {
File file = new File(targetFile);
FileOutputStream fos = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
osw.write(newContent);
osw.flush();
}
private static String readFile(String originFile) throws IOException {
FileInputStream fis = new FileInputStream(originFile);
final InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
BufferedReader bufferedReader = new BufferedReader(isr);
String content = "";
String line = null;
while ((line = bufferedReader.readLine()) != null) {
content += line + "\r\n";
}
return content;
}
}
我是长路,感谢你的耐心阅读。如有问题请指出,我会积极采纳!
欢迎关注我的公众号【长路Java】,分享Java学习文章及相关资料
Q群:851968786 我们可以一起探讨学习
注明:转载可,需要附带上文章链接