Spring boot Spring引导任务计划删除父文件夹中的数百万个旧文件

Spring boot Spring引导任务计划删除父文件夹中的数百万个旧文件,spring-boot,Spring Boot,想要在50到300天之间删除数百万个旧文件,如果我使用简单的spring任务,那么删除带有文件夹的文件也会有性能开销。要删除再次需要递归方法的文件夹 什么方法比较好,有什么建议或解决方案。我不确定我是否完全理解你的问题,但下面是一个Spring Boot应用程序中的计划任务示例,它从根路径开始递归查找所有文件,然后删除过去50天内未修改的任何文件 此任务每10秒运行一次 @Service public class FileService { @Scheduled(fixedDelay

想要在50到300天之间删除数百万个旧文件,如果我使用简单的spring任务,那么删除带有文件夹的文件也会有性能开销。要删除再次需要递归方法的文件夹


什么方法比较好,有什么建议或解决方案。

我不确定我是否完全理解你的问题,但下面是一个Spring Boot应用程序中的计划任务示例,它从根路径开始递归查找所有文件,然后删除过去50天内未修改的任何文件

此任务每10秒运行一次

@Service
public class FileService {

    @Scheduled(fixedDelay = 10000)
    public void deleteFilesScheduledTask() throws IOException {
        findFiles("C:/testing");
    }

    public void findFiles(String filePath) throws IOException {
        List<File> files = Files.list(Paths.get(filePath))
                                .map(path -> path.toFile())
                                .collect(Collectors.toList());
        for(File file: files) {
            if(file.isDirectory()) {
                findFiles(file.getAbsolutePath());
            } else if(isFileOld(file)){
                deleteFile(file);
            }
        }

    }

    public void deleteFile(File file) {
        file.delete();
    }

    public boolean isFileOld(File file) {
        LocalDate fileDate = Instant.ofEpochMilli(file.lastModified()).atZone(ZoneId.systemDefault()).toLocalDate();
        LocalDate oldDate = LocalDate.now().minusDays(50);
        return fileDate.isBefore(oldDate);
    }
}
@服务
公共类文件服务{
@计划(固定延迟=10000)
public void deleteFileScheduledTask()引发IOException{
FindFile(“C:/testing”);
}
公共void findFiles(字符串文件路径)引发IOException{
List files=files.List(path.get(filePath))
.map(路径->路径.toFile())
.collect(Collectors.toList());
用于(文件:文件){
if(file.isDirectory()){
FindFile(file.getAbsolutePath());
}else if(isFileOld(文件)){
删除文件(文件);
}
}
}
公共void deleteFile(文件文件){
delete();
}
公共布尔值isFileOld(文件){
LocalDate fileDate=Instant.ofEpochMilli(file.lastModified()).atZone(ZoneId.systemDefault()).toLocalDate();
LocalDate oldDate=LocalDate.now().minusDays(50);
返回fileDate.isBefore(oldDate);
}
}

希望这能让您了解如何在自己的应用程序中实现此功能。

我不确定是否完全理解您的问题,但下面是一个Spring Boot应用程序中的计划任务示例,该任务从根路径开始递归查找所有文件,然后删除过去50天内未修改的所有文件

此任务每10秒运行一次

@Service
public class FileService {

    @Scheduled(fixedDelay = 10000)
    public void deleteFilesScheduledTask() throws IOException {
        findFiles("C:/testing");
    }

    public void findFiles(String filePath) throws IOException {
        List<File> files = Files.list(Paths.get(filePath))
                                .map(path -> path.toFile())
                                .collect(Collectors.toList());
        for(File file: files) {
            if(file.isDirectory()) {
                findFiles(file.getAbsolutePath());
            } else if(isFileOld(file)){
                deleteFile(file);
            }
        }

    }

    public void deleteFile(File file) {
        file.delete();
    }

    public boolean isFileOld(File file) {
        LocalDate fileDate = Instant.ofEpochMilli(file.lastModified()).atZone(ZoneId.systemDefault()).toLocalDate();
        LocalDate oldDate = LocalDate.now().minusDays(50);
        return fileDate.isBefore(oldDate);
    }
}
@服务
公共类文件服务{
@计划(固定延迟=10000)
public void deleteFileScheduledTask()引发IOException{
FindFile(“C:/testing”);
}
公共void findFiles(字符串文件路径)引发IOException{
List files=files.List(path.get(filePath))
.map(路径->路径.toFile())
.collect(Collectors.toList());
用于(文件:文件){
if(file.isDirectory()){
FindFile(file.getAbsolutePath());
}else if(isFileOld(文件)){
删除文件(文件);
}
}
}
公共void deleteFile(文件文件){
delete();
}
公共布尔值isFileOld(文件){
LocalDate fileDate=Instant.ofEpochMilli(file.lastModified()).atZone(ZoneId.systemDefault()).toLocalDate();
LocalDate oldDate=LocalDate.now().minusDays(50);
返回fileDate.isBefore(oldDate);
}
}

希望这能让您了解如何在自己的应用程序中实现此功能。

感谢您的帮助,我有以下删除文件的代码&它工作正常,但是删除文件后,还需要删除嵌套文件夹,删除嵌套文件夹时遇到困难。并且还需要知道我是否应该使用任务线程池进行并行/多线程处理,以便性能得到改善.Files.list(path).filter(n->(getLastModifiedTimeUnchecked(n)>=from&&getLastModifiedTimeUnchecked(n){System.out.println(n);delete(n,(t,u)->System.err.format(“无法删除%s%n”,t,u.getMessage());});公共静态长getLastModifiedTimeUnchecked(路径路径路径)抛出uncheckDioException{try{long lastTime=Files.getLastModifiedTime(路径).toMillis();return lastTime;}catch(IOException ex){抛出新的uncheckDioException(ex);}publicstaticvoiddelete(Path,biconsumere){try{Files.delete(Path);}catch(IOException ex){e.accept(Path,ex);}}感谢您的帮助,我有以下删除文件的代码&它工作正常,但是删除文件后,还需要删除嵌套文件夹,删除嵌套文件夹有困难。我还需要知道我是否应该使用任务线程池进行并行/多线程处理,以便提高性能。files.list(path)。filter(n->(getLastModifiedTimeUnchecked(n)>=from&&getLastModifiedTimeUnchecked(n){System.out.println(n);delete(n,(t,u)->System.err.format(“无法删除%s%n”,t,u.getMessage());});公共静态长getLastModifiedTimeUnchecked(路径路径)抛出UncheckedIOException{try{long lastTime=Files.getLastModifiedTime(path).toMillis();return lastTime;}catch(IOException ex){throw new UncheckedIOException(ex);}}}public static void delete(path path,BiConsumer e){try{Files.delete(path);}catch(IOException ex){e.accept(路径,ex);}