演示BufferedOutputStream和BufferedInputStream
-
buffer
(1)bufferInputStream
(2)bufferOutputStream
使用它们,可以完成二进制文件拷贝,也可以操作文本文件
package com.io.outputstream_; import java.io.*; /** * 演示BufferedOutputStream和BufferedInputStream.关闭时也只需关闭外层的处理流,底层节点流会自动关闭 */ public class BufferedCopy02 { public static void main(String[] args) { String srcPath="d:\\cat.jpg"; String destPath="d:\\cat3.jpg"; BufferedInputStream bis=null; BufferedOutputStream bos=null; //创建BufferedOutputStream对象和BufferedInputStream对象 try { //因为FileInputStream是InputStream子类 bis=new BufferedInputStream(new FileInputStream(srcPath)); bos=new BufferedOutputStream(new FileOutputStream(destPath)); //循环读取文件,并写入到destPath byte[] buff=new byte[1024]; int readLen=0; //当返回-1时表示文件读取完毕 while ((readLen=bis.read(buff))!=-1){ bos.write(buff,0,readLen); } System.out.println("文件拷贝完毕"); } catch (IOException e) { e.printStackTrace(); } finally { try { if (bis!=null){ bis.close(); } if (bos!=null){ bos.close(); } } catch (IOException e) { e.printStackTrace(); } } } }