本帖最后由 王红霞 于 2012-7-20 09:50 编辑
import java.io.*;
class FileCopy
{
public static void main(String[] args)
{
FileCopyed();
}
public static void FileCopyed()
{
FileReader fr=null;
FileWriter fw=null;
try
{
fr=new FileReader("Test3.java");
fw=new FileWriter("file_copy.txt");
char[] buffer=new char[1024];
int length=0;
while((length=fr.read(buffer))!=-1)
{
fw.write(buffer,0,length);
System.out.println(new String(buffer,0,length));
}
}
catch(IOException e)
{
throw new RuntimeException(e);
}
finally
{
if(fr!=null)//两个程序的结果是一样的 。对fr、fw的判空有什么意义?既然下面的程序也可以成功运行,那是不是就是说不需要判空?
{
try
{
fr.close();
}
catch(IOException e)
{
throw new RuntimeException(e);
}
}
if(fw!=null)
{
try
{
fw.close();
}
catch(IOException e)
{
throw new RuntimeException(e);
}
}
}
}
}
------------------------------------------------------------------------------------------------------
import java.io.*;
class FileCopy
{
public static void main(String[] args)
{
FileCopyed();
}
public static void FileCopyed()
{
FileReader fr=null;
FileWriter fw=null;
try
{
fr=new FileReader("Test3.java");
fw=new FileWriter("file_copy.txt");
char[] buffer=new char[1024];
int length=0;
while((length=fr.read(buffer))!=-1)
{
fw.write(buffer,0,length);
System.out.println(new String(buffer,0,length));
}
}
catch(IOException e)
{
throw new RuntimeException(e);
}
finally
{
//没有判空
try
{
fr.close();
}
catch(IOException e)
{
throw new RuntimeException(e);
}
//没有判空
try
{
fw.close();
}
catch(IOException e)
{
throw new RuntimeException(e);
}
}
}
}
|