IO流 - 复制文件(字节缓冲流+字节流)

一、什么是缓冲流

缓冲流的内部都包含一个缓冲区,通过缓冲区读写,可以提高了IO流的读写速度。

二、缓冲流+字节流复制文件

//明确数据源
        FileInputStream fis=new FileInputStream("D:\\java1018\\buffer.txt");
        //明确目的地
        FileOutputStream fos=new FileOutputStream("D:\\java1018\\a\\buffer2.txt");
        //创建缓冲流对象
        //输入
        BufferedInputStream bis=new BufferedInputStream(fis);
        //输出
        BufferedOutputStream bos=new BufferedOutputStream(fos);
        
        int len=0;
        //1.单字节复制
        while ((len=bis.read())!=-1) {
            bos.write(len);
            
        }
        //2.字节数组复制
        byte [] bytes=new byte[1024];
        while ((len=bis.read(bytes))!=-1) {
            bos.write(bytes,0,len);
            
        }
        //释放资源
        bis.close();
        bos.close();

猜你喜欢

转载自www.cnblogs.com/l1314/p/12361009.html