码迷,mamicode.com
首页 > 其他好文 > 详细

IO流 拷贝图片

时间:2015-05-08 14:28:22      阅读:80      评论:0      收藏:0      [点我收藏+]

标签:

/*

 * 复制一个图片:  

* 1、用字节读取流对象和图片关联  

* 2、用字节写入流对象创建一个图像文件,用于存储获取到的图片  

* 3、通过循环读写,完成数据的存储  

* 4、关闭资源

 */

import java.io.*;

public class CopyPic {  

         public static void main(String[] args)

            { 

                   FileOutputStream fos=null;  

                   FileInputStream fis=null;   

                   try  

                      {   

                              fos=new FileOutputStream("c:\\2.bmp");    

                              fis=new FileInputStream("c:\\1.bmp");   

                              byte[] buf=new byte[1024];   

                              int len=0;    

                             while((len=fis.read(buf))!=-1)    

                                 {   

                                       fos.write(buf, 0, len);  

                                  }  

                      }  

                catch(IOException e)  

                     {    

                         throw new RuntimeException("复制文件失败");  

                      }  

                 finally   

                    {    

                            try   

                                   {    

                                         if(fis!=null)     

                                         fis.close();    

                                    }   

                            catch(IOException e)    

                                   {    

                                         throw new RuntimeException("读取关闭失败");   

                                    }    

                            try   

                                  {   

                                        if(fos!=null)     

                                        fos.close();  

                                  }   

                         catch(IOException e)    

                                {    

                                     throw new RuntimeException("写入关闭失败");  

                                 }  

                    }

          }

}

IO流 拷贝图片

标签:

原文地址:http://www.cnblogs.com/binglin/p/4487410.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!