字节缓冲流——Java

字节缓冲流:

BufferedOutputStream:字节缓冲输出流。

BufferedInputStream:字节缓冲输入流。

构造方法:

BufferedOutputStream(OutputStream out)

BufferedInputStream(InputStream in)

构造方法需要的是字节流,而不是具体的文件或路径。

字节缓冲流仅提供缓冲区,真正的读写数据还得依靠基本的字节流对象进行操作。

使用字节缓冲进行复制

private static void demo05() throws IOException {
    
    
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream("E:\\Steve.pdf"));
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("Steve.pdf"));

    byte[] bytes = new byte[1024];
    int i = 0;
    while ((i = bis.read(bytes)) != -1) {
    
    
        bos.write(bytes, 0, i);
    }
    bos.close();
    bis.close();
}

小结:

字节缓冲流相当于货车,需要传入一个字节流也就是司机进行操作。

通过字节缓冲,提高效率。

猜你喜欢

转载自blog.csdn.net/qq_45022687/article/details/122592826