根据文件路径获取路径下的文件和文件夹,遍历删除文件,遍历删除文件夹
程序员文章站
2024-03-17 12:09:22
...
给定一个指定的路径,遍历这个路径下的所有文件和文件夹,并分类。
删除3天前的文件和空文件夹。
package com.cknow.schedule.cronjobs;
import com.cknow.beans.config.FileImageConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@Component
@Configuration //1.主要用于标记配置类,兼备Component的效果。
@EnableScheduling // 2.开启定时任务
public class ScheduleTaskDeleteFiles {
@Autowired
private FileImageConfig fileImageConfig;
/**
* 定时删除临时文件夹下的文件
* <p>
* {秒数} {分钟} {小时} {日期} {月份} {星期} {年份(可为空)}
*/
@Scheduled(cron = "5 * * * * ?") //5秒钟执行一次
private void deleteFileTasks() {
//String tempPath = fileImageConfig.getImageTempFolder();
String tempPath = "D:\\cknow\\temp_dev";
ArrayList<File> fileList = new ArrayList<File>();
ArrayList<File> fileDirectoryList = new ArrayList<File>();
Map<String,ArrayList<File>> map = getFilesAndDirectory(fileList,fileDirectoryList,tempPath);
ArrayList<File> fileListResult = map.get("fileList");
ArrayList<File> fileDirectoryListResult = map.get("fileDirectoryList");
System.out.println(fileListResult.size());
System.out.println(fileDirectoryListResult.size());
deleteFiles(fileListResult);
deleteDirectory(fileDirectoryListResult);
}
/**
* 方法getFilesAndDirectory根据指定路径获取所有的文件和文件夹
*/
private Map<String,ArrayList<File>> getFilesAndDirectory(ArrayList<File> fileList, ArrayList<File> fileDirectoryList, String path) {
try {
//目标集合fileList
File file = new File(path);
if (file.isDirectory()) {
File[] files = file.listFiles();
for (File fileIndex : files) {
//如果这个文件是目录,则进行递归搜索
if (fileIndex.isDirectory()) {
//fileDirectoryList.add(fileIndex);//由最外层到最内层,不利于删除
getFilesAndDirectory(fileList,fileDirectoryList,fileIndex.getPath());
fileDirectoryList.add(fileIndex);//由最内层到最外层
} else {
//如果文件是普通文件,则将文件句柄放入集合中
fileList.add(fileIndex);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
Map<String,ArrayList<File>> map = new HashMap<>();
map.put("fileList",fileList);//文件集合
map.put("fileDirectoryList",fileDirectoryList);//文件夹集合
return map;
}
/**
* 删除文件
* @param files
*/
private void deleteFiles(ArrayList<File> files){
if (files == null || files.size() == 0){
return;
}
Long currentTime = System.currentTimeMillis();
for (int i = 0; i < files.size(); i++) {
File file = files.get(i);
Long lastModified = file.lastModified();
System.out.print(lastModified);
Date date = new Date(lastModified);
System.out.print("\t");
System.out.print(date);
System.out.print("\t");
Long cha = currentTime - lastModified;
double result = cha * 1.0 /(1000*60*60);
System.out.print("cha="+result);
System.out.print("\t");
System.out.println("删除文件="+file.getPath());
if (result >= 72 ){//大于72小时,删除文件;就是删除3天前的文件
file.delete();
}
}
}
/**
* 删除文件夹
* @param directory
*/
private void deleteDirectory(ArrayList<File> directory){
if (directory == null || directory.size() == 0){
return;
}
for (int i = 0; i < directory.size(); i++) {
File file = directory.get(i);
if(file.isDirectory()){
if (file.list().length > 0){
System.out.println("目录下不为空,不删除"+file.getPath());
} else {
System.out.println("目录下为空,删除"+file.getPath());
file.delete();
}
}
}
}
public static void main(String[] args) {
new ScheduleTaskDeleteFiles().deleteFileTasks();
}
}
如果你觉得对你有帮助,请支持一下。☺