org.apache.commons.io.FileUtils's copyDirectory(sourceDirectory, destDirectory) method can also be used for copying Directory from source to destination.
Program to Copy a Directory - using org.apache.commons.io.FileUtils's copyDirectory(sourceDirectory, destDirectory)- java file IO >
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
/** JavaMadeSoEasy.com */
public class CopyDirectoryUsingCommonsJar {
public static void main(String... args) {
String sourceDirectoryName = "c:/myDirectory";
File sourceDirectory = new File(sourceDirectoryName);
String destDirectoryName = "c:/myDirectoryCopy";
File destDirectory = new File(destDirectoryName);
try {
FileUtils.copyDirectory(sourceDirectory, destDirectory);
System.out.println(sourceDirectoryName + " copied successfully to "
+ destDirectoryName);
} catch (IOException e) {
e.printStackTrace();
}
}
}
/* OUTPUT
c:/myDirectory copied successfully to c:/myDirectoryCopy
*/
|
Note : In order to avoid java.io.FileNotFoundException you must use java.io.File's isDirectory() method to ensure that sourceDirectoryName is a directory. I haven't used it just to keep the code simple.
RELATED LINKS>
Program to Create Directory - Single and multiple (i.e. parent and child directories) in java file IO
Program to Identify File or Directory - in java file IO | And in java 7 using java.nio.file
Traverse a Directory (all sub-directories and files) in 2 ways
Program to Write BYTE data type in file using DataOutputStream and Read BYTE from file using DataInputStream - java file IO
Labels:
Core Java
File IO/File handling