提交 ec591009 编写于 作者: Q qinyingjie

fix:删除多余的图片

上级 5b88436c
package com.kwan.springcloudalibaba.util;
import com.google.common.base.Charsets;
import com.google.common.io.Files;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.StopWatch;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
/**
* 字符串是否存在文件中
*
* @author : qinyingjie
* @version : 2.2.0
* @date : 2023/2/8 10:45
*/
@Slf4j
public class FileContains {
private static boolean IS_EXIST = false;
public static void main(String[] args) throws IOException {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
final List<String> isNotExist = new ArrayList<>();
//获取picPath下面所有的文件名
String picPath = "/Users/qinyingjie/Documents/idea-workspace/blogimg/";
final List<String> picNames = getPicName(picPath);
log.info("图片总数为{}", picNames.size());
for (String word : picNames) {
IS_EXIST = false;
//遍历某个文件夹下的所有以.vue结尾的文件
//博客文件夹
String folder = "/Users/qinyingjie/Documents/idea-workspace/blog/";
//指定类型的文件
String suffix = ".md";
//包含某个字符串
traverseFolder(folder, suffix, word);
if (!IS_EXIST) {
isNotExist.add(word);
}
}
log.info("不存在图片总数为{}", isNotExist.size());
for (String s : isNotExist) {
deletePic(picPath + s);
}
stopWatch.stop();
//毫秒输出
log.info(String.valueOf(stopWatch.getTime(TimeUnit.MILLISECONDS)));
}
/**
* 获取文件
*
* @param path
* @param suffix
* @param word
* @throws IOException
*/
public static void traverseFolder(String path, String suffix, String word) throws IOException {
File file = new File(path);
if (file.exists()) {
//获取文件夹下的文件
File[] files = file.listFiles();
if (null != files && files.length != 0) {
for (File file2 : files) {
//是否是文件夹
if (file2.isDirectory()) {
traverseFolder(file2.getAbsolutePath(), suffix, word);
} else {
//包含md结尾的文件
if (file2.getAbsolutePath().contains(suffix)) {
getParams(file2.getAbsolutePath(), word);
}
}
}
}
}
}
/**
* 判断文件是否存在
*
* @param classPath
* @param word
* @throws IOException
*/
public static void getParams(String classPath, String word) throws IOException {
File file = new File(classPath);
//每行作为一个字符串,存为列表元素
List<String> strings = Files.readLines(file, Charsets.UTF_8);
for (String string : strings) {
//判断是否包含方法名称,即指定字符串
if (string.contains(word)) {
//文件存在
IS_EXIST = true;
}
}
}
/**
* 获取图片名称
*
* @param path
* @return
*/
public static List<String> getPicName(String path) {
List<String> picNames = new ArrayList<>();
File file = new File(path);
if (file.exists()) {
//获取文件夹下的文件
File[] files = file.listFiles();
if (null != files && files.length != 0) {
for (File file2 : files) {
//是否是文件夹
if (!file2.isDirectory()) {
//包含md结尾的文件
final String name = file2.getName();
picNames.add(name);
}
}
}
}
return picNames;
}
/**
* 删除文件
*
* @param picPath
*/
public static void deletePic(String picPath) {
File file = new File(picPath);
try {
file.delete();
System.out.printf("删除文件成功:%s%n", picPath);
} catch (Exception e) {
System.err.printf("无法删除的路径 %s%n%s", picPath, e);
}
}
}
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册