1 2 import java.awt.*; 3 import java.awt.event.*; 4 import javax.swing.*; 5 import java.io.*; 6 7 void copyFile(File sSourceFileName, File sTargetFileName) 8 //copies files from one location to another 9 throws FileNotFoundException, IOException 10 { 11 FileInputStream aFileInputStream = new FileInputStream(sSourceFileName); 12 FileOutputStream aFileOutputStream = new FileOutputStream(sTargetFileName); 13 synchronized (aFileInputStream) 14 { 15 synchronized (aFileOutputStream) 16 { 17 byte [] buffer = new byte[256]; 18 while (true) 19 { 20 int bytesread = aFileInputStream.read(buffer); 21 if (bytesread == -1) 22 { 23 break; 24 } 25 aFileOutputStream.write(buffer, 0, bytesread); 26 } 27 } 28 } 29 aFileInputStream.close(); 30 aFileOutputStream.flush(); 31 aFileOutputStream.close(); 32 } 33 34 // Copies all files under srcDir to dstDir. 35 // If dstDir does not exist, it will be created. 36 void copyDirectory(File srcDir, File dstDir) throws IOException { 37 if (srcDir.isDirectory()) { 38 if (!dstDir.exists()) { 39 dstDir.mkdir(); 40 } 41 42 String[] children = srcDir.list(); 43 for (int i=0; i<children.length; i++) { 44 copyDirectory(new File(srcDir, children[i]), 45 new File(dstDir, children[i])); 46 } 47 } else { 48 49 copyFile(srcDir, dstDir); 50 } 51 } 52 53 void jButton2_actionPerformed(ActionEvent e) { 54 55 File sFile1 = new File("C:\\Test1\\"); 56 File sFile2 = new File("C:\\Test2\\"); 57 58 //Call to copy files from directory to another 59 try { 60 61 copyDirectory(sFile1 ,sFile2); 62 } catch (IOException e1) { 63 } 64 65 }