23.IO学习——字节流(2)——图片复制…

来源:互联网 发布:php微信h5支付demo 编辑:程序博客网 时间:2024/06/04 20:07

字节复制图片重点:

4.复制图片(重点)
 
  需求:
    1)即有读又有写
    2)还是非文本数据
    使用到了字节流中用于操作文件的对象。
  实现:
    
 //复制图片
 //1.明确数据的来源
 FileInputStream fis=newFileInputStream("title.png");

 FileOutputStream fos=newFileOutputStream("tt.png");

 //2.自定义缓冲区
 byte[] buf=new byte[1024];

 int len=0;
 while ((len=fis.read(buf))!=-1)
 {
  fos.write(buf,0,len);
 }

 fos.close();
 fis.close();

5.缓冲区
  BufferedInputStream
  BufferedOutputStream

  示例:复制图片
              //缓冲区(速度快)
  FileInputStream fis=newFileInputStream("title.png");

  FileOutputStream fos=newFileOutputStream("ttt.png");
    
  BufferedInputStream bfis=newBufferedInputStream(fis);
  BufferedOutputStream bfos=newBufferedOutputStream(fos);
  
   int len=0;
  while((len=bfis.read())!=-1)
  {
   bfos.write(len);
  }
  bfis.close();
  bfos.close();

 

0 0