From f7ce82cfab0608b724d58db2d65feee503f09971 Mon Sep 17 00:00:00 2001 From: Bunny <1319900154@qq.com> Date: Fri, 2 Aug 2024 00:10:56 +0800 Subject: [PATCH] =?UTF-8?q?:rocket:=20feat(=E6=96=B0=E5=A2=9E):=20?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E8=BE=93=E5=85=A5=E8=BE=93=E5=87=BA=E8=AF=BB?= =?UTF-8?q?=E5=8F=96=E5=92=8C=E5=86=99=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- service/src/test/java/cn/bunny/FileTest.java | 33 ------ .../src/test/java/cn/bunny/file/FileTest.java | 108 ++++++++++++++++++ .../java/cn/bunny/{ => zip}/ZipFIleTest.java | 2 +- .../cn/bunny/{ => zip}/ZipOutFIleTest.java | 2 +- 4 files changed, 110 insertions(+), 35 deletions(-) delete mode 100644 service/src/test/java/cn/bunny/FileTest.java create mode 100644 service/src/test/java/cn/bunny/file/FileTest.java rename service/src/test/java/cn/bunny/{ => zip}/ZipFIleTest.java (97%) rename service/src/test/java/cn/bunny/{ => zip}/ZipOutFIleTest.java (97%) diff --git a/service/src/test/java/cn/bunny/FileTest.java b/service/src/test/java/cn/bunny/FileTest.java deleted file mode 100644 index ea6d39f..0000000 --- a/service/src/test/java/cn/bunny/FileTest.java +++ /dev/null @@ -1,33 +0,0 @@ -package cn.bunny; - -import lombok.SneakyThrows; - -import java.io.FileInputStream; -import java.io.IOException; -import java.util.zip.ZipEntry; -import java.util.zip.ZipInputStream; - -public class FileTest { - @SneakyThrows - public static void main(String[] args) { - String zipFilePath = "D:\\MyFolder\\文档和软件\\图片.zip"; - try (FileInputStream fis = new FileInputStream(zipFilePath); - ZipInputStream zis = new ZipInputStream(fis)) { - - ZipEntry entry = zis.getNextEntry(); - while (entry != null) { - System.out.println("正在读取: " + entry.getName()); - - // 这里可以添加代码来读取entry的内容 - // 例如,如果entry是一个文件,你可以使用BufferedReader来读取文本文件的内容 - - // 读取下一个条目 - entry = zis.getNextEntry(); - } - } catch (IOException e) { - e.printStackTrace(); - } - // File file = new File("D:\\MyFolder\\文档和软件\\图片.zip"); - // ZipFile zipFile = new ZipFile(file); - } -} diff --git a/service/src/test/java/cn/bunny/file/FileTest.java b/service/src/test/java/cn/bunny/file/FileTest.java new file mode 100644 index 0000000..520c29d --- /dev/null +++ b/service/src/test/java/cn/bunny/file/FileTest.java @@ -0,0 +1,108 @@ +package cn.bunny.file; + +import org.junit.jupiter.api.Test; + +import java.io.*; +import java.nio.channels.FileChannel; +import java.time.Duration; +import java.time.LocalDateTime; + +public class FileTest { + /** + * * 读取和写入纯文本内容 + */ + @Test + void paintTextTest() { + // 源文件路径 + String source = "D:\\dir\\source\\plain text.md"; + // 目标文件路径 + String target = "D:\\dir\\target\\plain text.md"; + + try ( + // 创建读取和写入流 + BufferedReader reader = new BufferedReader(new FileReader(source)); + BufferedWriter writer = new BufferedWriter(new FileWriter(target)) + ) { + // 读取源文件并写入目标文件 + String line = reader.readLine(); + while (line != null) { + writer.write(line); + writer.newLine(); + line = reader.readLine(); + } + } catch (IOException exception) { + System.out.println(exception.getMessage()); + } + } + + /** + * * 读取和写入流媒体内容 + */ + @Test + void streamMediaTest() { + // 开始复制时间 + LocalDateTime start = LocalDateTime.now(); + + // 源文件路径 + String sourcePath = "D:\\dir\\source\\video.ts"; + // 目标文件路径 + String targetPath = "D:\\dir\\target\\video.ts"; + + try ( + // 创建FileInputStream来读取源文件 + FileInputStream reader = new FileInputStream(sourcePath); + // 创建BufferedWriter来写入目标文件 + FileOutputStream writer = new FileOutputStream(targetPath) + ) { + // 创建缓冲区 + byte[] buffer = new byte[1024 * 7]; + int length; + + // 读取源文件并写入目标文件 + while ((length = reader.read(buffer)) > 0) writer.write(buffer, 0, length); + + } catch (IOException exception) { + System.out.println(exception.getMessage()); + } + + // 计算时间差 + LocalDateTime end = LocalDateTime.now(); + Duration duration = Duration.between(start, end); + System.out.println("复制耗时: " + duration.toMillis() + " 毫秒");// 复制耗时: 614 毫秒 + } + + /** + * * 使用NIO + */ + @Test + void nioStreamMediaTest() { + // 开始复制时间 + LocalDateTime start = LocalDateTime.now(); + + // 源文件路径 + String sourcePath = "D:\\dir\\source\\video.ts"; + // 目标文件路径 + String targetPath = "D:\\dir\\target\\video.ts"; + + try ( + FileInputStream inputStream = new FileInputStream(sourcePath); + FileOutputStream outputStream = new FileOutputStream(targetPath); + FileChannel sourceChannel = inputStream.getChannel(); + FileChannel targetChannel = outputStream.getChannel() + ) { + long position = 0; + long count = sourceChannel.size(); + + while (position < count) { + position += sourceChannel.transferTo(position, count - position, targetChannel); + } + } catch (IOException exception) { + System.out.println(exception.getMessage()); + } + + // 计算时间差 + LocalDateTime end = LocalDateTime.now(); + Duration duration = Duration.between(start, end); + System.out.println("复制耗时: " + duration.toMillis() + " 毫秒");// 复制耗时: 325 毫秒 + } +} diff --git a/service/src/test/java/cn/bunny/ZipFIleTest.java b/service/src/test/java/cn/bunny/zip/ZipFIleTest.java similarity index 97% rename from service/src/test/java/cn/bunny/ZipFIleTest.java rename to service/src/test/java/cn/bunny/zip/ZipFIleTest.java index 41a1dfd..d4d18e9 100644 --- a/service/src/test/java/cn/bunny/ZipFIleTest.java +++ b/service/src/test/java/cn/bunny/zip/ZipFIleTest.java @@ -1,4 +1,4 @@ -package cn.bunny; +package cn.bunny.zip; import java.io.FileOutputStream; import java.io.IOException; diff --git a/service/src/test/java/cn/bunny/ZipOutFIleTest.java b/service/src/test/java/cn/bunny/zip/ZipOutFIleTest.java similarity index 97% rename from service/src/test/java/cn/bunny/ZipOutFIleTest.java rename to service/src/test/java/cn/bunny/zip/ZipOutFIleTest.java index c6221f9..b9c4cfd 100644 --- a/service/src/test/java/cn/bunny/ZipOutFIleTest.java +++ b/service/src/test/java/cn/bunny/zip/ZipOutFIleTest.java @@ -1,4 +1,4 @@ -package cn.bunny; +package cn.bunny.zip; import java.io.FileOutputStream; import java.io.IOException;