import java.io.*;
class CopyText
{
public static void main(String[] args) throws IOException
{
copy_2();
}
public static void copy_2()
{
FileWriter w=null;//为什么都要先定义为空啊???
FileReader r=null;
try
{
w=new FileWriter("MapDemo_copy.txt");
r=new FileReader("MapDemo.java");
char []buf=new char[1024];//1024什么意思啊 ??
int len=0;
while ((len=r.read(buf))!=-1)
{
w.write(buf,0,len);
}
}
catch (IOException e)
{
throw new RuntimeException("复制失败");
}
finally
{
if(r!=null)//为什么还要判断啊,不都要关闭资源吗???
try
{
r.close();
}
catch (IOException e)
{
System.out.println("错误");
}
if(w!=null)
try
{
w.close();
}
catch (IOException e)
{
System.out.println("错误");
}
}
}
|