Complete approach was introduced in java nio folder copy mechanism as of Java 7.  In copying a folder recursively using java io packages, what we do is to list all files on the source directory and then check if there child directories. and then we copy files as we move along. Mean to say in old method, we are browsing the internal structure of the source directory and copying the contents as we move along the folders. Now with java nio, the walkFileTree method of java.nio.file.Files class enable us to browse directly on the source directory and copy the contents as we move along. Not only that we are able to do this on java API’s, we also have readily available copy options like REPLACE_EXISTING and COPY_ATTRIBUTES. From the name itself of the static variables mentioned, we are only specifying that we want to replace existing files and copy the source attributes.

My java nio folder copy tutorial is limited functionality only which only covers basic file tree walking and copying of files recursively. However, the example below will give the backbone of more complicated copying mechanism.

JAVA NIO Folder copy using walkFileTree

This sample source code shows how to copy files recursively using the java nio packages. Copying includes the source attributes and replace the existing files. This source code is basically an advance version of my java tutorial on how to copy a single file using java.nio.

package com.javatutorialhq.tutorial;

/**
 * This source code is for copying a folder or files recursively using java nio
 *
 */
import java.nio.file.*;
import static java.nio.file.StandardCopyOption.*;
import java.nio.file.attribute.*;
import static java.nio.file.FileVisitResult.*;
import java.io.IOException;
import java.util.*;

public class FolderCopyNIO {
	public static Path source = Paths.get("E:/tmp/java/tutorial/nio/file/copy/source");
	public static Path target = Paths.get("D:/target");

	public static void main(String[] args) throws IOException {
		//java nio folder copy
		EnumSet options = EnumSet.of(FileVisitOption.FOLLOW_LINKS);
		//check first if source is a directory
		if(Files.isDirectory(source)){
			System.out.println("source is a directory");

			Files.walkFileTree(source, options, Integer.MAX_VALUE, new FileVisitor() {

				@Override
				public FileVisitResult postVisitDirectory(Path dir,
						IOException exc) throws IOException {
					//System.out.println(source);
					return FileVisitResult.CONTINUE;
				}

				@Override
				public FileVisitResult preVisitDirectory(Path dir,
						BasicFileAttributes attrs)  {
					CopyOption[] opt = new CopyOption[]{COPY_ATTRIBUTES,REPLACE_EXISTING};
					System.out.println("Source Directory "+dir);
					Path newDirectory = target.resolve(source.relativize(dir));
					System.out.println("Target Directory "+newDirectory);
					try{
						System.out.println("creating directory tree "+Files.copy(dir, newDirectory,opt));
					}
					catch(FileAlreadyExistsException x){
					}
					catch(IOException x){
						return FileVisitResult.SKIP_SUBTREE;
					}

					return CONTINUE;
				}

				@Override
				public FileVisitResult visitFile(Path file,
						BasicFileAttributes attrs) throws IOException {
					//System.out.println("results");
					System.out.println("Copying file:"+file);
					kopya(file, target.resolve(source.relativize(file)));
					return CONTINUE;
				}

				@Override
				public FileVisitResult visitFileFailed(Path file,
						IOException exc) throws IOException {
					return CONTINUE;
				}
			});
		}

	}
	public static void kopya(Path source,Path target) throws IOException{
		CopyOption[] options = new CopyOption[]{REPLACE_EXISTING,COPY_ATTRIBUTES};
		System.out.println("Copied file "+Files.copy(source, target,options));

	}
}

Sample Output

source is a directory
Source Directory E:\tmp\java\tutorial\nio\file\copy\source
Target Directory D:\target
creating directory tree D:\target
Copying file:E:\tmp\java\tutorial\nio\file\copy\source\sample text.txt
Copied file D:\target\sample text.txt
Source Directory E:\tmp\java\tutorial\nio\file\copy\source\teknoscope
Target Directory D:\target\teknoscope
creating directory tree D:\target\teknoscope
Copying file:E:\tmp\java\tutorial\nio\file\copy\source\teknoscope\excel source.xlsx
Copied file D:\target\teknoscope\excel source.xlsx
Source Directory E:\tmp\java\tutorial\nio\file\copy\source\teknoscope\folder
Target Directory D:\target\teknoscope\folder
creating directory tree D:\target\teknoscope\folder
Source Directory E:\tmp\java\tutorial\nio\file\copy\source\teknoscope\folder\1
Target Directory D:\target\teknoscope\folder\1
creating directory tree D:\target\teknoscope\folder\1
Copying file:E:\tmp\java\tutorial\nio\file\copy\source\teknoscope\sample.au3
Copied file D:\target\teknoscope\sample.au3
Copying file:E:\tmp\java\tutorial\nio\file\copy\source\test.txt
Copied file D:\target\test.txt
E:\tmp\java\tutorial\nio\file\copy\source