Java使用Files复制和移动文件
使用流方式写入写出,参考:juejin.cn/post/739804…
复制文件
// 开始复制时间
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 毫秒
移动文件
// 开始复制时间
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 毫秒