字节缓冲流

字节缓冲流

BufferOutputStream 字节缓冲输出流

BufferInputStream 字节缓冲输入流

构造方法

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

底层原理

//就要利用缓冲流去拷贝文件

    // 创建一个字节缓冲输入流
    // 在底层创建了一个默认长度为8192的字节数组
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\a.png"));
    // 创建一个字节输出流
    //  在底层创建了一个默认长度为8192的字节数组
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\b.png"));
    int b;
    while((b = bis.read()) != -1){
        bos.write(b);
    }
    // 方法的底层会把输出流关闭
    bis.close();
    bos.close();
}

字节数组&缓冲流

public static void main(String[] args) throws IOException {
        // 缓冲流结合数组,进行文件拷贝

        // 创建一个字节缓冲输入流
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\a.png"));
        // 创建一个字节缓冲输出流
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\b.png"));

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