This post will discuss how to move all files from one directory to another in Java.

1. Using FileUtils.moveDirectory() method

For copying or moving a directory to another location, you can use third-party libraries like Apache Commons IO whose FileUtils class offers several file manipulation utilities.

The following solution uses the FileUtils.copyDirectory() method to copy a whole directory to a new location. This method is hugely overloaded to preserve the file dates, apply a filter on a directory, etc.

Download Code

2. Using FileUtils.moveDirectory() method

The copyDirectory() method only copies the directory. To moves a directory, you can use the FileUtils.moveDirectory() method (or FileUtils.moveDirectoryToDirectory() method). You can use it as follows:

Download Code

3. Using FileSystemUtils.copyRecursively() method

The Spring Framework provides various utility methods for working with the file system. You can use the FileSystemUtils.copyRecursively() method to recursively copy the contents of the source directory to the destination directory. It is overloaded to accept both java.io.File and java.nio.file.Path.

Download Code

That’s all about moving all files from one directory to another in Java.