• java 删除所有早于N天的文件(递归选项,它遍历子文件夹)


    public static void recursiveDeleteFilesOlderThanNDays(int days, String dirPath) throws IOException {
        long cutOff = System.currentTimeMillis() - (days * 24 * 60 * 60 * 1000);
        Files.list(Paths.get(dirPath))
        .forEach(path -> {
            if (Files.isDirectory(path)) {
                try {
                    recursiveDeleteFilesOlderThanNDays(days, path.toString());
                } catch (IOException e) {
                    // log here and move on
                }
            } else {
                try {
                    if (Files.getLastModifiedTime(path).to(TimeUnit.MILLISECONDS) < cutOff) {
                        Files.delete(path);
                    }
                } catch (IOException ex) {
                    // log here and move on
                }
            }
        });
    }
    
  • 相关阅读:
    符号表
    经典算法-堆排序
    AngularJs基础
    Echars详解
    经典算法-快速排序
    经典算法-递归
    异常分类,异常抛出位置
    js的数据类型具体分析
    函数
    数据
  • 原文地址:https://www.cnblogs.com/interdrp/p/15640921.html
Copyright © 2020-2023  润新知