copy file using FileReader/Writer.

时间:2023-03-08 23:28:16
copy file using FileReader/Writer.

The code below demonstates copying file using 'FileReader' and 'FileWriter'.

 class CopyV2 extends Timer
{
public void runCode()
{
File fSrc = new File("f.txt");
File fDes = new File("f_des.txt");
Reader r = null;
Writer w = null;
try
{
r = new FileReader(fSrc);
w = new FileWriter(fDes);
int num = -1; //the character read.
while((num = r.read()) != -1)
w.write((char)num);
}
catch (FileNotFoundException e)
{
fDes.delete();
println(e);
}
catch(IOException e)
{ }
finally
{
try
{
if(r != null)
r.close();
}
catch (IOException e)
{
} try
{
if(w != null)
w.close();
}
catch (IOException e)
{
}
}
}
}

upon the code,'Timer' is a class defined by myself,which was used to calculate the elapsed time using 'template method pattern'.

and the figure below simply drawn the relevantcopy file using FileReader/Writer. steps: