黑马程序员技术交流社区

标题: 拷贝图片文件 [打印本页]

作者: 德胜    时间: 2015-9-16 09:05
标题: 拷贝图片文件
                        

/*拷贝图片文件                                                        
思路:1、用字节读取流对象和文件向关联 ;                                       
          2、用字节写入流对象创建一个新文件,用于存储获取的图片数据;                          
          3、循环读写,完成数据存储;                                          
          4、关闭资源。                                                
*/                                                           
public class picDemo {                                       
public static void main (String[] args)                       
        {                                                         
        FileOutputStream fos = null;                              
        FileIntputStream fis = null;                              
        try                                                      
        {                                                         
                fos = FileOutputStream("c:\\2.bmp");                  
                fis = 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 RuntimeException("读取文件失败");                 
                }                                                     
                try                                                   
                {                                                     
                        if(fos!=null)                                    
                                fos.close();                                 
                }                                                     
                catch(IOException e)                                 
                {                                                     
                        throw RuntimeException("写入文件失败");                 
                }                                                     
        }                                                         
        }                                                         

}                                                            






欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2