Java删除过期文件
2024-10-07 17:55:22
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();
}
}
最新文章
- gevent
- mysql习惯及主从复制参数设置
- wechat开发
- Java基础(4):Scanner输入的典型应用
- matlab 椭圆方程拟合
- 洛谷P1457 城堡 The Castle
- CURL: CURLE_COULDNT_CONNECT问题探究
- Spring Named Parameters examples in SimpleJdbcTemplate
- wxWidgets刚開始学习的人导引(2)——下载、安装wxWidgets
- 高级NAT-DMZ配置 -虚拟主机配置
- VMware驱动程序";vmci.sys";的版本不正确 怎么解决
- 机器学习--kNN算法识别手写字母
- Chapter 4 Invitations——26
- How to Apply Patches to a WLS 8.1 Environment
- Maven安装与配置及使用
- Linux 删除用户,用户主目录
- 使用RabbitMq
- 手把手教你搭APM之Skywalking搭建指南(支持Java/C#/Node.js)
- 开发中遇到的一些mongoose的问题
- 平衡二叉树Balanced Binary Tree
热门文章
- Sublime Text3怎样在Deepin中配置CTags插件
- 2018-8-29-win2d-渐变颜色
- linux随笔-04
- 在linux中出现there are stopped jobs 的解决方法【转自:http://www.linuxdiyf.com/viewarticle.php?id=104604】
- 力扣—Remove Nth Node From End of List(删除链表的倒数第N个节点) python实现
- C语言之——__attribute__
- Dubbo底层原理
- MUI 自定义从底部弹出的弹出框内容
- 【软工项目Beta阶段】第11周Scrum会议博客
- express框架总结