From 7506ade44f07bc4d6b0d843b2abedffe034fa189 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A7=A6=E8=8B=B1=E6=9D=B0?= <327782001@qq.com> Date: Wed, 22 Mar 2023 15:49:26 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E6=B7=BB=E5=8A=A0=E5=B7=A5=E5=85=B7?= =?UTF-8?q?=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 15 ++ .../com/kwan/spring5/utils/FileContains.java | 150 ++++++++++++++++++ src/main/resources/spring1.xml | 4 - src/test/java/Spring_01_BookTest.java | 1 - 4 files changed, 165 insertions(+), 5 deletions(-) create mode 100644 src/main/java/com/kwan/spring5/utils/FileContains.java diff --git a/pom.xml b/pom.xml index f1f6651..72ac854 100644 --- a/pom.xml +++ b/pom.xml @@ -73,5 +73,20 @@ 5.3.8 compile + + cn.hutool + hutool-all + 5.8.12 + + + com.alibaba.fastjson2 + fastjson2 + 2.0.23 + + + com.google.guava + guava + 31.1-jre + \ No newline at end of file diff --git a/src/main/java/com/kwan/spring5/utils/FileContains.java b/src/main/java/com/kwan/spring5/utils/FileContains.java new file mode 100644 index 0000000..02862af --- /dev/null +++ b/src/main/java/com/kwan/spring5/utils/FileContains.java @@ -0,0 +1,150 @@ +package com.kwan.spring5.utils; + + +import cn.hutool.core.date.StopWatch; +import com.alibaba.fastjson2.JSON; +import com.google.common.base.Charsets; +import com.google.common.io.Files; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +/** + * 字符串是否存在文件中 + * + * @author : qinyingjie + * @version : 2.2.0 + * @date : 2023/2/8 10:45 + */ +public class FileContains { + + /** + * 默认不存在 + */ + private static boolean IS_EXIST = false; + /** + * 图片路径 + */ + private static final String PIC_PATH = "/Users/qinyingjie/Documents/idea-workspace/blogimg/"; + /** + * 博客路径 + */ + private static final String BLOG_FOLDER = "/Users/qinyingjie/Documents/idea-workspace/blog/"; + + public static void main(String[] args) throws IOException { + StopWatch stopWatch = new StopWatch(); + stopWatch.start(); + final List isNotExist = new ArrayList<>(); + //获取picPath下面所有的文件名 + final List picNames = getPicName(PIC_PATH); + System.out.println("图片总数为" + picNames.size()); + for (String word : picNames) { + IS_EXIST = false; + //指定类型的文件 + String suffix = ".md"; + //包含某个字符串 + traverseFolder(BLOG_FOLDER, suffix, word); + //文件不存在 + if (!IS_EXIST) { + isNotExist.add(word); + deletePic(PIC_PATH + word); + } + } + System.out.println("不存在图片总数为" + isNotExist.size()); + stopWatch.stop(); + //毫秒输出 + System.out.println(JSON.toJSONString(stopWatch.getTaskInfo())); + } + + /** + * 获取文件 + * + * @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 strings = Files.readLines(file, Charsets.UTF_8); + for (String string : strings) { + //判断是否包含方法名称,即指定字符串 + if (string.contains(word)) { + //文件存在 + IS_EXIST = true; + } + } + } + + /** + * 获取图片名称 + * + * @param path + * @return + */ + public static List getPicName(String path) { + List 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 diff --git a/src/main/resources/spring1.xml b/src/main/resources/spring1.xml index 2967571..5814835 100644 --- a/src/main/resources/spring1.xml +++ b/src/main/resources/spring1.xml @@ -9,7 +9,6 @@ - - - \ No newline at end of file diff --git a/src/test/java/Spring_01_BookTest.java b/src/test/java/Spring_01_BookTest.java index 2040437..2b0ab1c 100644 --- a/src/test/java/Spring_01_BookTest.java +++ b/src/test/java/Spring_01_BookTest.java @@ -11,7 +11,6 @@ import org.springframework.context.support.ClassPathXmlApplicationContext; * @date : 2022/11/12 18:43 */ public class Spring_01_BookTest { - @Test public void test1() { ApplicationContext ctx = -- GitLab