How to Copy Directory in Java Recursively
In this post we will see How to Copy Directory in Java Recursively.
I have a directory “kscodes\source” and it has following folders and files inside it.
Lets see an example which will copy from “kscodes\source” to “kscodes\destination”
The example shows a method
copyDirectory(File sourceDir, File destinationDir) that is called recursively to copy directories and files from source.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | package com.kscodes.sampleproject; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; public class CopyDirExample { public static void main(String[] args) { String sourceDirName = "C:\\kscodes\\source"; String destionationDirName = "C:\\kscodes\\destination"; File sourceDir = new File(sourceDirName); File destinationDir = new File(destionationDirName); System.out.println("Start !!!!"); copyDirectory(sourceDir, destinationDir); System.out.println("End !!!"); } public static void copyDirectory(File sourceDir, File destinationDir) { // Create Destination director if it doesn't exist if (!destinationDir.exists()) { destinationDir.mkdir(); System.out.println("Directory " + destinationDir.getAbsolutePath() + " created !!!"); } // get all Files and directories for the given path String files[] = sourceDir.list(); for (String file : files) { File sourceFile = new File(sourceDir, file); File destinationFile = new File(destinationDir, file); // recursion :: to copy all the inner directories and files if (sourceFile.isDirectory()) { copyDirectory(sourceFile, destinationFile); } else { copyFile(sourceFile, destinationFile); } } } public static void copyFile(File sourceFile, File destinationFile) { InputStream input = null; OutputStream output = null; try { input = new FileInputStream(sourceFile); output = new FileOutputStream(destinationFile); byte[] buf = new byte[1024]; int bytesRead; while ((bytesRead = input.read(buf)) > 0) { output.write(buf, 0, bytesRead); } System.out.println("File " + sourceFile.getAbsolutePath() + " copied to " + destinationFile.getAbsolutePath()); } catch (Exception e) { } finally { try { input.close(); output.close(); } catch (Exception e) { } } } } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | Start !!!! File C:\kscodes\source\file1.txt copied to C:\kscodes\destination\file1.txt File C:\kscodes\source\file2.txt copied to C:\kscodes\destination\file2.txt Directory C:\kscodes\destination\testDirectoryOne created !!! File C:\kscodes\source\testDirectoryOne\OneDirFile1.txt copied to C:\kscodes\destination\testDirectoryOne\OneDirFile1.txt File C:\kscodes\source\testDirectoryOne\OneDirFile2.txt copied to C:\kscodes\destination\testDirectoryOne\OneDirFile2.txt Directory C:\kscodes\destination\testDirectoryThree created !!! File C:\kscodes\source\testDirectoryThree\ThreeDirFile1.txt copied to C:\kscodes\destination\testDirectoryThree\ThreeDirFile1.txt File C:\kscodes\source\testDirectoryThree\ThreeDirFile2.txt copied to C:\kscodes\destination\testDirectoryThree\ThreeDirFile2.txt File C:\kscodes\source\testDirectoryThree\ThreeDirFile3.txt copied to C:\kscodes\destination\testDirectoryThree\ThreeDirFile3.txt Directory C:\kscodes\destination\testDirectoryTwo created !!! File C:\kscodes\source\testDirectoryTwo\TwoDirFile1.txt copied to C:\kscodes\destination\testDirectoryTwo\TwoDirFile1.txt File C:\kscodes\source\testDirectoryTwo\TwoDirFile2.txt copied to C:\kscodes\destination\testDirectoryTwo\TwoDirFile2.txt End !!! |