mirror of
https://github.com/Snailclimb/JavaGuide
synced 2025-06-16 18:10:13 +08:00
Update io-basis.md
JAVA IO基础知识总结中的字节缓冲流,对复制一个PDF文件的举例测试代码,位置相反了
This commit is contained in:
parent
16b86763b6
commit
71404dcfbd
@ -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
|
```java
|
||||||
@Test
|
@Test
|
||||||
void copy_pdf_to_another_pdf_with_byte_array_buffer_stream() {
|
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(字节缓冲输入流)
|
||||||
|
|
||||||
`BufferedInputStream` 从源头(通常是文件)读取数据(字节信息)到内存的过程中不会一个字节一个字节的读取,而是会先将读取到的字节存放在缓存区,并从内部缓冲区中单独读取字节。这样大幅减少了 IO 次数,提高了读取效率。
|
`BufferedInputStream` 从源头(通常是文件)读取数据(字节信息)到内存的过程中不会一个字节一个字节的读取,而是会先将读取到的字节存放在缓存区,并从内部缓冲区中单独读取字节。这样大幅减少了 IO 次数,提高了读取效率。
|
||||||
|
Loading…
x
Reference in New Issue
Block a user