diff --git a/service/src/test/java/cn/bunny/file/FileTest.java b/service/src/test/java/cn/bunny/file/FileStreamTest.java similarity index 70% rename from service/src/test/java/cn/bunny/file/FileTest.java rename to service/src/test/java/cn/bunny/file/FileStreamTest.java index 520c29d..57e8ebe 100644 --- a/service/src/test/java/cn/bunny/file/FileTest.java +++ b/service/src/test/java/cn/bunny/file/FileStreamTest.java @@ -7,7 +7,7 @@ import java.nio.channels.FileChannel; import java.time.Duration; import java.time.LocalDateTime; -public class FileTest { +public class FileStreamTest { /** * * 读取和写入纯文本内容 */ @@ -36,7 +36,7 @@ public class FileTest { } /** - * * 读取和写入流媒体内容 + * * 读取和写入流媒体内容,使用自己的缓冲区 */ @Test void streamMediaTest() { @@ -55,12 +55,12 @@ public class FileTest { FileOutputStream writer = new FileOutputStream(targetPath) ) { // 创建缓冲区 - byte[] buffer = new byte[1024 * 7]; + byte[] buffer = new byte[1024 * 1024 * 10]; int length; // 读取源文件并写入目标文件 while ((length = reader.read(buffer)) > 0) writer.write(buffer, 0, length); - + // reader.transferTo(writer); } catch (IOException exception) { System.out.println(exception.getMessage()); } @@ -68,7 +68,38 @@ public class FileTest { // 计算时间差 LocalDateTime end = LocalDateTime.now(); Duration duration = Duration.between(start, end); - System.out.println("复制耗时: " + duration.toMillis() + " 毫秒");// 复制耗时: 614 毫秒 + System.out.println("复制耗时: " + duration.toMillis() + " 毫秒");// 复制耗时: 1298 毫秒 + } + + /** + * * 使用流的 transferTo + */ + @Test + void streamMediaTransferToTest() { + // 开始复制时间 + LocalDateTime start = LocalDateTime.now(); + + // 源文件路径 + String sourcePath = "D:\\dir\\source\\video.ts"; + // 目标文件路径 + String targetPath = "D:\\dir\\target\\video.ts"; + + try ( + // 创建FileInputStream来读取源文件 + FileInputStream inputStream = new FileInputStream(sourcePath); + // 创建BufferedWriter来写入目标文件 + FileOutputStream outputStream = new FileOutputStream(targetPath) + ) { + // 将当前输入流中所有字节传输到指定的输出流 + inputStream.transferTo(outputStream); + } catch (IOException exception) { + System.out.println(exception.getMessage()); + } + + // 计算时间差 + LocalDateTime end = LocalDateTime.now(); + Duration duration = Duration.between(start, end); + System.out.println("复制耗时: " + duration.toMillis() + " 毫秒");// 复制耗时: 767 毫秒 } /** @@ -103,6 +134,6 @@ public class FileTest { // 计算时间差 LocalDateTime end = LocalDateTime.now(); Duration duration = Duration.between(start, end); - System.out.println("复制耗时: " + duration.toMillis() + " 毫秒");// 复制耗时: 325 毫秒 + System.out.println("复制耗时: " + duration.toMillis() + " 毫秒");// 复制耗时: 772 毫秒 } } diff --git a/service/src/test/java/cn/bunny/file/FilesTest.java b/service/src/test/java/cn/bunny/file/FilesTest.java new file mode 100644 index 0000000..db643ea --- /dev/null +++ b/service/src/test/java/cn/bunny/file/FilesTest.java @@ -0,0 +1,50 @@ +package cn.bunny.file; + +import lombok.SneakyThrows; +import org.junit.jupiter.api.Test; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.time.Duration; +import java.time.LocalDateTime; + +public class FilesTest { + // 需要保证复制目录下文件不存在,否则会报错 + @SneakyThrows + @Test + void copyFileTest() { + // 开始复制时间 + 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 毫秒 + } + + @SneakyThrows + @Test + void moveFileTest() { + // 开始复制时间 + 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 毫秒 + } +}