本帖最后由 騛鹏 于 2013-3-22 18:03 编辑
- import java.io.*;
- class CopyText
- {
- public static void copy_2()
- {
- FileWriter fw = null;
- FileReader fr = null;
- try
- {
- fw = new FileWriter("SystemDemo_copy.txt");
- fr = new FileReader("SystemDemo.java");
- char[] buf = new char[1024];
- int len = 0;
- while ((len=fr.read(buf))!=-1)
- {
- fw.write(buf,0,len);
- }
- }
- catch (IOException e)
- {
- throw new RuntimeException("no read or write");
- }
- finally
- {
- //------------------------------------
- if(fr!=null)
- try
- {
- fr.close();
- }
- catch (IOException e)
- {
- }
- if(fw!=null)
- try
- {
- fw.close();
- }
- catch (IOException e)
- {
- }
- //----------------------------------------
- }
- }
- public static void main(String[] args)
- {
- copy_2();
- }
- }
复制代码 问题:
finally中 能否写为: 有什么区别?- try
- {
- if(fr!=null)
- fr.close();
- }
- catch (IOException e)
- {
- }
- try
- {
- if(fw!=null)
- fw.close();
- }
- catch (IOException e)
- {
- }
复制代码 |