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 } } }); }