从日期和日期为 springboot 的文件夹中删除

问题描述 投票:0回答:0

//Delete from folder with to date and from date springbootDelete from folder with to date and from date springbootDelete from folder with to date and from 日期springboot

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
public class FileDeletionExample {

    public static void main(String[] args) {
        String folderPath = "path/to/folder"; // Specify the folder path
        Date fromDate = /* specify the start date */;
        Date toDate = /* specify the end date */;

        File folder = new File(folderPath);
        File[] files = folder.listFiles();

        if (files != null) {
            for (File file : files) {
                if (file.isFile()) {
                    Date lastModifiedDate = new Date(file.lastModified());

                    if (lastModifiedDate.after(fromDate) && lastModifiedDate.before(toDate)) {
                        if (file.delete()) {
                            System.out.println("Deleted file: " + file.getName());
                        } else {
                            System.out.println("Failed to delete file: " + file.getName());
                        }
                    }
                }
            }
        }
    }
}
java file spring-boot-actuator
© www.soinside.com 2019 - 2024. All rights reserved.