黑马程序员技术交流社区

标题: io流遇到的问题,想的头疼 [打印本页]

作者: 魏亮    时间: 2012-10-12 16:39
标题: io流遇到的问题,想的头疼
本帖最后由 魏亮 于 2012-10-12 16:42 编辑
  1. import java.io.*;
  2. class  CopyPic
  3. {
  4.         public static void main(String[] args)
  5.         {
  6.                 FileInputStream fis = null;
  7.                 FileOutputStream fos = null;
  8.                 try
  9.                 {
  10.                         fis = new FileInputStream("pic.bmp");
  11.                         fos = new FileOutputStream("pic_2.bmp");
  12.                         
  13.                      <FONT color=yellowgreen> int ch = 0;
  14.                         while ((ch=fis.read())!=-1)
  15.                         {
  16.                                 fos.write(ch);
  17.                         }      
  18. </FONT>              <FONT color=red>//上边的的方法程序停止,光标闪烁,复制的图片文件无法读取,为什么? 我检查了很多遍,实在想不出??
  19.               /*下边的方法可以复制成功
  20. </FONT>                     <FONT color=yellowgreen>  byte[] buf = new byte[1024];
  21.                         int len=0;
  22.                         while((len=fis.read(buf))!=-1)
  23.                         {
  24.                                 fos.write(buf,0,len);
  25.                         }
  26. </FONT>                    <FONT color=red>  * /
  27. </FONT>                catch (IOException e)
  28.                 {
  29.                         System.out.println(e.toString());
  30.                 }
  31.                 finally
  32.                 {
  33.                         try
  34.                         {
  35.                                 if(fis!=null)
  36.                                         fis.close();
  37.                         }
  38.                         catch (IOException e)
  39.                         {
  40.                                 System.out.println(e.toString());
  41.                         }
  42.                         try
  43.                         {
  44.                                 if(fos!=null)
  45.                                         fis.close();
  46.                         }
  47.                         catch (IOException e)
  48.                         {
  49.                                 System.out.println(e.toString());
  50.                         }
  51.                 }
  52.         }
  53.         
  54. }
复制代码

作者: 张 涛    时间: 2012-10-12 16:47
write方法后面要及时flush,才能写到文件中。
作者: 徐梦侠    时间: 2012-10-12 18:01
代码没有问题吧,你的代码除了第一个try没有结束的}外,代码都能够正常运行,复制的图片没有问题,你再重新复制出来试一下呢。
作者: 杨志男    时间: 2012-10-12 20:18
  1. package com.ithima.practice;

  2. import java.io.*;

  3. class CopyPic {
  4.         public static void main(String[] args) {
  5.                 FileInputStream fis = null;
  6.                 FileOutputStream fos = null;
  7.                 try {
  8.                         fis = new FileInputStream("pic.bmp");
  9.                         fos = new FileOutputStream("pic_2.bmp");
  10.                         int ch = 0;
  11.                         while ((ch = fis.read()) != -1) {
  12.                                 fos.write(ch);
  13.                         }
  14.                 }
  15.                 /*
  16.                  * byte[] buf = new byte[1024]; int len=0;
  17.                  * while((len=fis.read(buf))!=-1) {fos.write(buf,0,len); }
  18.                  */
  19.                 catch (IOException e) {
  20.                         System.out.println(e.toString());
  21.                 } finally {
  22.                         try {
  23.                                 if (fis != null)
  24.                                         fis.close();
  25.                         } catch (IOException e) {
  26.                                 System.out.println(e.toString());
  27.                         }
  28.                         try {
  29.                                 if (fos != null)
  30.                                         fis.close();
  31.                         } catch (IOException e) {
  32.                                 System.out.println(e.toString());
  33.                         }
  34.                 }
  35.         }
  36. }
复制代码
完全可以啊,亲,亲测的
作者: 魏亮    时间: 2012-10-15 09:42
可以????我再试试
作者: 魏亮    时间: 2012-10-15 10:39
这个真的没有问题,我等了一段时间就复制成功了,肯能是我电脑太慢了,劳驾上面各位
作者: 陈军    时间: 2012-10-15 11:36
上面没有用flush方法刷新。。下面有close(),这个方法关闭前会有刷新动作  {:soso_e100:}




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