IO流10 --- 缓冲流(字节型)实现非文本文件的复制 --- 技术搬运工(尚硅谷)
- 字节型缓冲流,BufferedOutputStream默认缓冲区大小 8192字节byte,满了自动flush()
@Test public void test6(){ File srcFile = new File("FLAMING MOUNTAIN.JPG"); File destFile = new File("FLAMING MOUNTAIN2.JPG"); FileInputStream fis = null; FileOutputStream fos = null; try { //节点流 fis = new FileInputStream(srcFile); fos = new FileOutputStream(destFile); //缓冲流 BufferedInputStream bis = new BufferedInputStream(fis); BufferedOutputStream bos = new BufferedOutputStream(fos); //复制文件 byte[] buffer = new byte[10]; int len; while ((len = bis.read(buffer)) != -1){ bos.write(buffer, 0, len); } } catch (IOException e) { e.printStackTrace(); }finally { //关闭外层流,内层自动关闭 if (bos != null){ try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } if (bis != null){ try { bis.close(); } catch (IOException e) { e.printStackTrace(); } } } }