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 毫秒
相关推荐
我爱娃哈哈6 分钟前
SpringBoot + XXL-JOB + Quartz:任务调度双引擎选型与高可用调度平台搭建
java·spring boot·后端
赛恩斯10 分钟前
kotlin 为什么可以在没有kotlin 环境的安卓系统上运行的
android·开发语言·kotlin
steem_ding11 分钟前
net.core 调优指南
开发语言·php
小宇的天下13 分钟前
Synopsys Technology File and Routing Rules Reference Manual (1)
java·服务器·前端
Coder_Boy_24 分钟前
基于SpringAI的在线考试系统-AI智能化拓展
java·大数据·人工智能·spring boot
n***333528 分钟前
TCP/IP协议栈深度解析技术文章大纲
java·spring boot
奋进的芋圆31 分钟前
Java 线程池深度指南(JDK 17+)
java
ScilogyHunter31 分钟前
SCons:Python驱动的智能构建系统
python·构建系统·scons