定时删除文件夹中的文件,可配置时间与文件夹路径,删除的文件为配置时间之前的文件
该段代码的需求为定时删除文件夹中的文件,可配置时间与文件夹路径,删除的文件为配置时间之前的文件
代码如下:
1.配置aplication.properties文件
#删除对应文件夹中的文件定时任务 开/关 on/off time.schedule.onoff=on #删除对应文件夹中的文件定时任务 定时cron表达式 time.schedule.cron=0 0 23 * * ?
2.Java代码实现定时
import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.util.ObjectUtils; import java.io.File; import java.io.IOException; import java.math.BigInteger; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.attribute.BasicFileAttributes; import java.time.Instant; import java.time.LocalDateTime; import java.util.Date; /** * 开发一个定时删除旧文件的代码,可配置时间与文件夹路径 * 删除的文件为配置时间之前的文件 */ @Configuration //1.主要用于标记配置类,兼备Component的效果。 @EnableScheduling // 2.开启定时任务 @Slf4j public class DeleteFileSche { @Value("${time.schedule.onoff}") private String onoff; //设置定时任务时间 @Scheduled(cron = "${time.schedule.cron}") public void configureTasks() { log.info("开始,当前线程名:"+Thread.currentThread().getName()+" start run Task "+ LocalDateTime.now()); if("on".equals(onoff) ){ long startTime = System.currentTimeMillis(); //获取开始时间 log.info("定时任务开始执行: " + LocalDateTime.now()); try { //设置日期 String s = "2022-02-17 16:20:00"; //日期格式 java.text.SimpleDateFormat format = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date d =format.parse(s); //设置文件夹路径 File file = new File("E://test//upload//222"); delteFile(file,d); } catch (Exception e) { log.error("定时任务执行报错:"+e.getMessage()); e.printStackTrace(); } long endTime = System.currentTimeMillis(); //获取结束时间 log.info("程序运行时间:" + (endTime - startTime) / 1000 + "s"); //输出程序运行时间 log.info("定时任务结束执行: " + LocalDateTime.now()); }else{ log.info("off".equals(onoff) ? "定时任务开关未打卡": "" ); } log.info("结束,当前线程名:"+Thread.currentThread().getName()+" end run Task "+ LocalDateTime.now()); } //删除设置时间之前该文件夹中的所有文件 public void delteFile(File file,Date d) { //判断该文件夹是否存在 if(!file.exists()){ log.info("文件夹不存在,请检查配置的文件路径是否正确!!!"); }else{ File[] filearray = file.listFiles(); StringBuilder stringBuilder = new StringBuilder(); if (filearray.length > 0) { for (File f : filearray) { //获取文件创建时间 BasicFileAttributes attr = null; Date time = null; try { Path path = f.toPath(); attr = Files.readAttributes(path, BasicFileAttributes.class); Instant instant = attr.creationTime().toInstant(); BigInteger milis = BigInteger.valueOf(instant.getEpochSecond()).multiply( BigInteger.valueOf(1000)); milis = milis.add(BigInteger.valueOf(instant.getNano()).divide( BigInteger.valueOf(1_000_000))); time = new Date(milis.longValue()); //获取文件修改时间 /* Calendar cal = Calendar.getInstance(); long time = f.lastModified(); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); cal.setTimeInMillis(time); Date c = cal.getTime(); System.out.println("c======"+c); System.out.println("文件修改时间==="+formatter.format(cal.getTime()));*/ if(d.compareTo(time) > 0){ f.delete(); //打印删除文件的文件名 if(ObjectUtils.isEmpty(stringBuilder)){ stringBuilder = stringBuilder.append(f.getName()); }else{ stringBuilder.append(",").append(f.getName()); } } } catch (IOException e) { e.printStackTrace(); } } log.info("文件删除成功,删除的文件名为:"+stringBuilder); }else{ log.info("文件删除失败,该文件夹下无文件,及文件夹为空!"); } } } }
final:不积跬步,无以至千里.不积小流,无以成江海

浙公网安备 33010602011771号