Copying Directories vs Files
What you'll learn: You'll understand that Files.copy() behaves very differently when applied to directories versus files.
When you use Files.copy() on a file, it copies the entire file content to a new location—exactly what you'd expect. But when you apply Files.copy() to a directory, it creates a shallow copy: an empty directory at the destination with the same name, but none of the contents inside.
Why This Matters
Think of Files.copy() on a directory like photocopying just the label on a folder, not the documents inside. You get a new folder structure, but it's empty. The method does not recursively descend into subdirectories or copy files within.
Path sourceDir = Paths.get("documents");
Path targetDir = Paths.get("documents_copy");
// This creates an EMPTY directory at documents_copy
Files.copy(sourceDir, targetDir);
// Files and subdirectories inside "documents" are NOT copied
After this operation, documents_copy exists as a directory, but it's completely empty—even if documents contained hundreds of files and folders.
What If You Need a Full Copy?
To copy a directory's entire contents, you must implement your own recursive logic: walk the directory tree, create each subdirectory, and copy each file individually using Files.copy() in a loop. The NIO.2 API provides tools for this (like directory walking), but Files.copy() itself doesn't do it automatically.
When Directory Copying Is Useful
Creating an empty directory copy is useful when you want to replicate a directory structure for organizational purposes without duplicating data—for instance, setting up parallel folder hierarchies for different environments.
Key Takeaway: Files.copy() on directories creates only an empty directory at the destination; it does not recursively copy contents. To copy entire directory trees, you must implement recursive logic yourself.