本帖最后由 杨玉揆 于 2011-12-10 12:58 编辑
不知道大家有没有考虑过关于文件之间的拷贝,对于先关那个流对象的问题?是先关读取流(fis.close())还是先关写入流(fos.close())呢?还是先关闭那个都一样?- import java.io.*;
- class PictureStream1
- {
- public static void main(String[] args)
- {
- FileOutputStream fos = null;
-
- FileInputStream fis = null;
-
- try
- {
- fos = new FileOutputStream("1.png");
- fis =new FileInputStream("Iterator.png");
- byte[] buf = new byte[1024];
- int len = 0;
- while((len = fis.read(buf))!= -1)
- {
- fos.write(buf);
- }
- }
- catch (IOException e)
- {
- throw new RuntimeException("读取失败");
- }
- finally
- {
- try
- {
- if(fos!=null)
- fos.close();
- }
- catch (IOException e)
- {
- throw new RuntimeException("关闭资源失败");
- }
- try
- {
- if(fis!=null)
- fis.close();
- }
- catch (IOException e)
- {
- throw new RuntimeException("关闭资源失败");
- }
-
-
- }
- }
- }
复制代码 该贴已经同步到 杨玉揆的微博 |