黑马程序员技术交流社区

标题: IO流练习-----图片拷贝代码 [打印本页]

作者: iFmmer    时间: 2015-6-18 22:46
标题: IO流练习-----图片拷贝代码
  1. import java.io.*;


  2. public class testPhotoCopy {
  3.         public static void main(String args[]){
  4.                 FileInputStream fis = null;
  5.                 FileOutputStream fos = null;
  6.                 try {
  7.                         fis = new FileInputStream("D:\\ashk_wnd.bmp");
  8.                         fos = new FileOutputStream("c:\\ashk_wnd.bmp");

  9.                         int num = 0;

  10.                         byte[] buf = new byte[1024];
  11.                        
  12.                         while ((num = fis.read(buf))!=-1){
  13.                                 fos.write(buf,0,num);
  14.                         }
  15.                        
  16.                 } catch (IOException e) {
  17.                         throw new RuntimeException("写入图片失败");
  18.                 } finally{
  19.                         try {
  20.                                 if(fis!=null)
  21.                                         fis.close();
  22.                         } catch (IOException e) {
  23.                                 throw new RuntimeException("关闭输入流失败");
  24.                         }
  25.                         try{
  26.                                 if(fis!=null)
  27.                                         fos.close();
  28.                         } catch(IOException e){
  29.                                 throw new RuntimeException("关闭输出流失败");
  30.                         }
  31.                 }

  32.         }
  33. }
复制代码







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