public static void main(String[] args) throws IOException {
long cut = LocalDateTime.now().minusWeeks(1).toEpochSecond(ZoneOffset.UTC);
Path path = Paths.get("D:\\test\\");
Files.list(path).filter(n -> getLastModifiedTimeUnchecked(n).to(TimeUnit.SECONDS) < cut)
.forEach(n -> {
System.out.println(n);
delete(n, (t, u) -> System.err.format("Couldn't delete %s%n", t, u.getMessage())
);
});
} public static FileTime getLastModifiedTimeUnchecked(Path path, LinkOption... options) throws UncheckedIOException {
try {
return Files.getLastModifiedTime(path, options);
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
} public static void delete(Path path, BiConsumer<Path, Exception> e) {
try {
System.out.println(path);
Files.delete(path);
} catch (IOException ex) {
e.accept(path, ex);
}
} public void deleteOldFile(){
long cut = LocalDateTime.now().minusDays(3L).toEpochSecond(ZoneOffset.UTC);
Path path = Paths.get("/path/to/delete");
try {
Files.list(path)
.filter(n -> {
try {
return Files.getLastModifiedTime(n).to(TimeUnit.SECONDS) < cut;
} catch (IOException ex) {
//handle exception
return false;
}
})
.forEach(n -> {
try {
Files.delete(n);
} catch (IOException ex) {
//handle exception
}
});
} catch (IOException e) {
e.printStackTrace();
}
}

最新文章

  1. gevent
  2. mysql习惯及主从复制参数设置
  3. wechat开发
  4. Java基础(4):Scanner输入的典型应用
  5. matlab 椭圆方程拟合
  6. 洛谷P1457 城堡 The Castle
  7. CURL: CURLE_COULDNT_CONNECT问题探究
  8. Spring Named Parameters examples in SimpleJdbcTemplate
  9. wxWidgets刚開始学习的人导引(2)——下载、安装wxWidgets
  10. 高级NAT-DMZ配置 -虚拟主机配置
  11. VMware驱动程序&quot;vmci.sys&quot;的版本不正确 怎么解决
  12. 机器学习--kNN算法识别手写字母
  13. Chapter 4 Invitations——26
  14. How to Apply Patches to a WLS 8.1 Environment
  15. Maven安装与配置及使用
  16. Linux 删除用户,用户主目录
  17. 使用RabbitMq
  18. 手把手教你搭APM之Skywalking搭建指南(支持Java/C#/Node.js)
  19. 开发中遇到的一些mongoose的问题
  20. 平衡二叉树Balanced Binary Tree

热门文章

  1. Sublime Text3怎样在Deepin中配置CTags插件
  2. 2018-8-29-win2d-渐变颜色
  3. linux随笔-04
  4. 在linux中出现there are stopped jobs 的解决方法【转自:http://www.linuxdiyf.com/viewarticle.php?id=104604】
  5. 力扣—Remove Nth Node From End of List(删除链表的倒数第N个节点) python实现
  6. C语言之——__attribute__
  7. Dubbo底层原理
  8. MUI 自定义从底部弹出的弹出框内容
  9. 【软工项目Beta阶段】第11周Scrum会议博客
  10. express框架总结