The code below demonstates copying file using 'FileReader' and 'FileWriter'.spa
1 class CopyV2 extends Timer 2 { 3 public void runCode() 4 { 5 File fSrc = new File("f.txt"); 6 File fDes = new File("f_des.txt"); 7 Reader r = null; 8 Writer w = null; 9 try 10 { 11 r = new FileReader(fSrc); 12 w = new FileWriter(fDes); 13 int num = -1; //the character read. 14 while((num = r.read()) != -1) 15 w.write((char)num); 16 } 17 catch (FileNotFoundException e) 18 { 19 fDes.delete(); 20 println(e); 21 } 22 catch(IOException e) 23 { 24 25 } 26 finally 27 { 28 try 29 { 30 if(r != null) 31 r.close(); 32 } 33 catch (IOException e) 34 { 35 } 36 37 try 38 { 39 if(w != null) 40 w.close(); 41 } 42 catch (IOException e) 43 { 44 } 45 } 46 } 47 }
upon the code,'Timer' is a class defined by myself,which was used to calculate the elapsed time using 'template method pattern'.code
and the figure below simply drawn the relevant steps:blog