Java使用Files复制和移动文件

Java使用Files复制和移动文件

使用流方式写入写出,参考:https://juejin.cn/post/7398046883644391459

复制文件

java 复制代码
// 开始复制时间
LocalDateTime start = LocalDateTime.now();

// 源文件路径
Path sourcePath = Path.of("D:\\dir\\source\\video.ts");
// 目标文件路径
Path targetPath = Path.of("D:\\dir\\target\\video.ts");

Files.copy(sourcePath, targetPath);

// 计算时间差
LocalDateTime end = LocalDateTime.now();
Duration duration = Duration.between(start, end);
System.out.println("复制耗时: " + duration.toMillis() + " 毫秒");// 复制耗时: 522 毫秒

移动文件

java 复制代码
// 开始复制时间
LocalDateTime start = LocalDateTime.now();

// 源文件路径
Path sourcePath = Path.of("D:\\dir\\source\\video.ts");
// 目标文件路径
Path targetPath = Path.of("D:\\dir\\target\\video.ts");

Files.move(sourcePath, targetPath);

// 计算时间差
LocalDateTime end = LocalDateTime.now();
Duration duration = Duration.between(start, end);
System.out.println("复制耗时: " + duration.toMillis() + " 毫秒");// 复制耗时: 0 毫秒
相关推荐
神仙别闹3 小时前
基于 C++ 实现(控制台)学生成绩管理系统
开发语言·c++
Y幽谷客3 小时前
Python加载本地大模型(Qwen3.5 8B)
python·大模型
Wang's Blog3 小时前
Java 接入Redis: 服务启动停止与后台运行配置
java·服务器·redis
此时不提桶,更待何时4 小时前
01-04-B-垃圾回收面试与生产事故实战
java·jvm·面试
伞伞悦读4 小时前
【第36期】Python 目录与路径详解:pathlib、文件遍历、创建、复制、移动和删除风险
开发语言·python
线上放牧人4 小时前
Windows删除图标缓存
windows·python·pyqt
qq_5470261795 小时前
Python 变量和简单数据类型
python
鹿角片ljp6 小时前
KV Cache 解析
java·算法
智搜广告6 小时前
智搜广告:科技行业AI回答优化公司如何破局
大数据·python·elasticsearch·geo