通过文件流复制图片

来源:互联网 发布:js 去掉数组重复元素 编辑:程序博客网 时间:2024/05/22 00:50
  1. import java.io.BufferedOutputStream;   
  2. import java.io.File;   
  3. import java.io.FileInputStream;   
  4. import java.io.FileNotFoundException;   
  5. import java.io.FileOutputStream;   
  6. import java.io.IOException;   
  7.   
  8. public class ImageCopier {   
  9.     
  10.  public static void main(String args[]) throws Exception{   
  11.   
  12.   
  13.   //源图片路径(包括图片名)   
  14.   String srcUrl = "D:/My Documents/temp/p1.jpg";   
  15.   
  16.   //目标图片路径   
  17.   String tarUrl = "D:/My Documents/temp/p2.jpg";   
  18.   ioImage(srcUrl, tarUrl);    
  19.  }   
  20.     
  21.  /**  
  22.   *   
  23.   * @param srcUrl 源图片路径  
  24.   * @return 目标图片路径  
  25.   */  
  26.  public static void ioImage(String srcUrl,String tarUrl){   
  27.        
  28.   try {   
  29.       
  30.    //生成读取图片到内存的输入流   
  31.    FileInputStream finput = new FileInputStream(new File(srcUrl));   
  32.       
  33.    //生成从内存将图片输出到磁盘的输出流   
  34.    FileOutputStream foutput = new FileOutputStream(new File(tarUrl));   
  35.    BufferedOutputStream bos = new BufferedOutputStream(foutput);   
  36.       
  37.    int b ;   
  38.    while(true) {       
  39.     if(finput.available()<1024){   
  40.      //   
  41.      while((b=finput.read())!=-1){   
  42.          
  43.       bos.write(b);   
  44.      }   
  45.      break;   
  46.     }else{   
  47.         
  48.      b=finput.read();   
  49.      bos.write(b);        
  50.     }       
  51.    }  finput.close();   
  52.    bos.close();   
  53.    foutput.close();   
  54.       
  55.   } catch (FileNotFoundException e) {   
  56.    // TODO Auto-generated catch block   
  57.    e.printStackTrace();   
  58.   } catch (IOException e) {   
  59.    // TODO Auto-generated catch block   
  60.    e.printStackTrace();   
  61.   }     
  62.      
  63.  }   
  64. }   
原创粉丝点击