From 71404dcfbd2d8b6754a04503797a0ac817ecb3e7 Mon Sep 17 00:00:00 2001 From: MrDecadent <82100995+MrDecadent@users.noreply.github.com> Date: Thu, 28 Jul 2022 15:16:01 +0800 Subject: [PATCH] Update io-basis.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit JAVA IO基础知识总结中的字节缓冲流,对复制一个PDF文件的举例测试代码,位置相反了 --- docs/java/io/io-basis.md | 102 +++++++++++++++++++-------------------- 1 file changed, 51 insertions(+), 51 deletions(-) diff --git a/docs/java/io/io-basis.md b/docs/java/io/io-basis.md index 16c83303..910532b3 100755 --- a/docs/java/io/io-basis.md +++ b/docs/java/io/io-basis.md @@ -303,6 +303,57 @@ BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputS 测试代码如下: +```java +@Test +void copy_pdf_to_another_pdf_buffer_stream() { + // 记录开始时间 + long start = System.currentTimeMillis(); + try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream("深入理解计算机操作系统.pdf")); + BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("深入理解计算机操作系统-副本.pdf"))) { + int content; + while ((content = bis.read()) != -1) { + bos.write(content); + } + } catch (IOException e) { + e.printStackTrace(); + } + // 记录结束时间 + long end = System.currentTimeMillis(); + System.out.println("使用缓冲流复制PDF文件总耗时:" + (end - start) + " 毫秒"); +} + +@Test +void copy_pdf_to_another_pdf_stream() { + // 记录开始时间 + long start = System.currentTimeMillis(); + try (FileInputStream fis = new FileInputStream("深入理解计算机操作系统.pdf"); + FileOutputStream fos = new FileOutputStream("深入理解计算机操作系统-副本.pdf")) { + int content; + while ((content = fis.read()) != -1) { + fos.write(content); + } + } catch (IOException e) { + e.printStackTrace(); + } + // 记录结束时间 + long end = System.currentTimeMillis(); + System.out.println("使用普通流复制PDF文件总耗时:" + (end - start) + " 毫秒"); +} +``` + +如果是调用 `read(byte b[])` 和 `write(byte b[], int off, int len)` 这两个写入一个字节数组的方法的话,只要字节数组的大小合适,两者的性能差距其实不大,基本可以忽略。 + +这次我们使用 `read(byte b[])` 和 `write(byte b[], int off, int len)` 方法,分别通过字节流和字节缓冲流复制一个 524.9 mb 的 PDF 文件耗时对比如下: + +``` +使用缓冲流复制PDF文件总耗时:695 毫秒 +使用普通字节流复制PDF文件总耗时:989 毫秒 +``` + +两者耗时差别不是很大,缓冲流的性能要略微好一点点。 + +测试代码如下: + ```java @Test void copy_pdf_to_another_pdf_with_byte_array_buffer_stream() { @@ -343,57 +394,6 @@ void copy_pdf_to_another_pdf_with_byte_array_stream() { } ``` -如果是调用 `read(byte b[])` 和 `write(byte b[], int off, int len)` 这两个写入一个字节数组的方法的话,只要字节数组的大小合适,两者的性能差距其实不大,基本可以忽略。 - -这次我们使用 `read(byte b[])` 和 `write(byte b[], int off, int len)` 方法,分别通过字节流和字节缓冲流复制一个 524.9 mb 的 PDF 文件耗时对比如下: - -``` -使用缓冲流复制PDF文件总耗时:695 毫秒 -使用普通字节流复制PDF文件总耗时:989 毫秒 -``` - -两者耗时差别不是很大,缓冲流的性能要略微好一点点。 - -测试代码如下: - -```java -@Test -void copy_pdf_to_another_pdf_buffer_stream() { - // 记录开始时间 - long start = System.currentTimeMillis(); - try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream("深入理解计算机操作系统.pdf")); - BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("深入理解计算机操作系统-副本.pdf"))) { - int content; - while ((content = bis.read()) != -1) { - bos.write(content); - } - } catch (IOException e) { - e.printStackTrace(); - } - // 记录结束时间 - long end = System.currentTimeMillis(); - System.out.println("使用缓冲流复制PDF文件总耗时:" + (end - start) + " 毫秒"); -} - -@Test -void copy_pdf_to_another_pdf_stream() { - // 记录开始时间 - long start = System.currentTimeMillis(); - try (FileInputStream fis = new FileInputStream("深入理解计算机操作系统.pdf"); - FileOutputStream fos = new FileOutputStream("深入理解计算机操作系统-副本.pdf")) { - int content; - while ((content = fis.read()) != -1) { - fos.write(content); - } - } catch (IOException e) { - e.printStackTrace(); - } - // 记录结束时间 - long end = System.currentTimeMillis(); - System.out.println("使用普通流复制PDF文件总耗时:" + (end - start) + " 毫秒"); -} -``` - ### BufferedInputStream(字节缓冲输入流) `BufferedInputStream` 从源头(通常是文件)读取数据(字节信息)到内存的过程中不会一个字节一个字节的读取,而是会先将读取到的字节存放在缓存区,并从内部缓冲区中单独读取字节。这样大幅减少了 IO 次数,提高了读取效率。