> For the complete documentation index, see [llms.txt](https://maxwell.gitbook.io/way-to-architect/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://maxwell.gitbook.io/way-to-architect/java-yu-yan/io/iofen-lei/1212.md).

# BufferdxxxxStream

```java
try (
        InputStream inputStream = new FileInputStream("/Users/yue/Desktop/test.ev4");
        InputStream bis = new BufferedInputStream(inputStream, 1024 * 1024);
        OutputStream outputStream = new FileOutputStream("/Users/yue/Desktop/copy.ev4");
        OutputStream bos = new BufferedOutputStream(outputStream, 1024 * 1024);
) {
    byte[] buffer = new byte[1024 * 1024];----①
    while ((bis.read(buffer)) != -1) {
        bos.write(buffer);
    }
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
```

对于BufferedInputStream，调用read()方法，虽然只是返回一个字节，但是一次会从系统中读取缓冲区大小（默认8192）的字节回来，下次再调用read()方法，如果字节在缓冲区内，会直接从缓冲区取，而不用调用系统的读取文件的方法。

对于没有缓冲功能的FileInputStream，每调用一次read()方法，都会触发系统的一次读取文件操作。

但是，如果我们使用read(byte\[] bys)方法，并且将bys的大小设置与缓冲区大小一致，对FileInputStream/BufferedInputStream两者而言，效率差不多。因为FileInputStream底层对read(byte\[] bys)做了优化，会一次从系统中读取bys数组大小的字节到内存中。

参考

[BufferedInputStream的read()方法为什么能够提高效率？](https://blog.csdn.net/zjkC050818/article/details/74611495)
