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 毫秒
相关推荐
Java后端的Ai之路11 小时前
RAG 还是 Agent 自主检索?Claude Code 删掉向量库之后,我把选型讲透了
开发语言·人工智能·prompt·agent·rag
starzy199011 小时前
深入理解Scala高阶函数:函数式编程核心
开发语言·scala
qq_1588954111114 小时前
AI 音乐生成完成后,音频 URL 为什么会过期?一套可恢复的交付方案
python·api·ai音乐
2601_9621066416 小时前
【Java Set综合实战】商品管理系统 + 图书收藏系统|Product、Book自定义类全套代码
java
Warson_L16 小时前
高性能架构设计
python·go
databook17 小时前
新数据到手,先跑哪几步检查
python·数据分析
RuoyiOffice17 小时前
SpringBoot+Vue3 凭证生命周期设计:审核、过账、冲销与反过账怎样保持账目一致
java·spring boot·事务·springboot3·财务系统·会计凭证·ruoyi office
2601_9621066417 小时前
HashMap入门|工号-姓名键值存储、两种遍历方式、根据key/value查
java
czq_268671948717 小时前
Python打卡第46天
开发语言·python
杨凯凡18 小时前
Spring Framework 7 API 版本管理实战:请求头路由、兼容迁移与废弃策略
java·rest api·springframework·spring boot 4·api版本管理