本帖最后由 清秋 于 2011-11-26 15:53 编辑
复制的第二种方法
当执行write()方法后,关闭流,为什么还需要执行close()方法?
fr==null- public static void copy_2()
- {
- FileWriter fw = null;
- FileReader fr = null;
- try
- {
- fw = new FileWriter("CopyText.txt");
- fr = new FileReader("CopyText.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("读写异常");
- }
- finally
- {
- if (fw!=null) //此处是否为空
- {
- try
- {
- fw.close();
- }
- catch (IOException e)
- {
- }
- }
- if (fr!=null)
- {
- try
- {
- fr.close();
- }
- catch (IOException e)
- {
- }
- }
- }
- }
复制代码 |
|