Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.io.File;

public class Main {
    public static boolean checkFilesIsNeedDelete(String path, long intervalTime) {
        File file = new File(path);
        if (file == null && !file.exists()) {
            return false;
        }
        if (file.isFile()) {
            deleteFileByIntervalTime(file, intervalTime);
        } else {
            File[] fileList = file.listFiles();
            for (int i = 0; i < fileList.length; i++) {
                File temp = fileList[i];
                if (temp.isDirectory()) {
                    checkFilesIsNeedDelete(temp.getAbsolutePath(), intervalTime);
                } else {
                    deleteFileByIntervalTime(temp, intervalTime);
                }
            }
        }
        return true;
    }

    public static boolean deleteFileByIntervalTime(File file, long intervalTime) {
        long time = System.currentTimeMillis();
        if (time - file.lastModified() > intervalTime) {
            file.delete();
        }
        return true;
    }
}