Move a directory in Java
This post will discuss how to move a directory in Java from one path to another.
There are several ways to move a directory in plain Java and using third-party libraries. These are discussed below in detail:
1. Using NIO
Since Java 1.7, JDK has java.nio.file.Files, which offers several static methods to operate on files and directories. We can use its move(Path source, Path target, CopyOption… options) method to move a directory.
This method takes the directory path to be moved, the path to the target directory, and an optional parameter to specify how the move is performed. By default, moving fails if the target directory already exists unless the REPLACE_EXISTING option is specified. If the source and target refer to the same directory, the method completes without moving the directory. If the source directory doesn’t exist, java.nio.file.NoSuchFileException is thrown.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.StandardCopyOption; class Main { public static void main(String[] args) { File from = new File("/var/www/src"); File to = new File("/var/www/dest"); try { Files.move(from.toPath(), to.toPath(), StandardCopyOption.REPLACE_EXISTING); System.out.println("Directory moved successfully."); } catch (IOException ex) { ex.printStackTrace(); } } } |
2. Using Apache Commons IO
Apache Commons IO’s FileUtils class has several utility methods for working with files. We can use its moveDirectory(File srcDir, File destDir) method to move a directory.
If the destination directory already exists, then this method throws org.apache.commons.io.FileExistsException. If the source directory doesn’t exist, java.io.FileNotFoundException is thrown. If an IO error occurs while moving the directory, java.io.IOException is thrown.
The major advantage of using Apache Commons IO is that it works even when the destination is on another file system.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import org.apache.commons.io.FileUtils; import java.io.File; import java.io.IOException; class Main { public static void main(String[] args) { File from = new File("/var/www/src"); File to = new File("/var/www/dest"); try { FileUtils.moveDirectory(from, to); System.out.println("Directory moved successfully."); } catch (IOException ex) { ex.printStackTrace(); } } } |
3. Using renameTo() method
We can also use the renameTo(File dest) method of the File class, which can move a directory within the same file system.
This method is platform-dependent, non-atomic, and fails if the destination exists beforehand. It doesn’t even throw an exception on failure. Therefore, we should always check the return value of this method to ensure that the move operation was successful.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.io.File; import java.io.IOException; class Main { public static void main(String[] args) { File from = new File("/var/www/src"); File to = new File("/var/www/dest"); boolean success = from.renameTo(to); if (success) { System.out.println("Directory Moved Successfully"); } else { System.out.println("Directory Moved Failed"); } } } |
4. Custom move routine
Finally, we can write our own routine to move a directory. The idea is to iterate over files in the source directory and individually move them. If the directory contains any subdirectories, perform this recursively. Here’s a working code:
|
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 |
import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardCopyOption; class Main { private static boolean moveDir(Path src, Path dest) { if (src.toFile().isDirectory()) { for (File file: src.toFile().listFiles()) { moveDir(file.toPath(), dest.resolve(src.relativize(file.toPath()))); } } try { Files.move(src, dest, StandardCopyOption.REPLACE_EXISTING); return true; } catch (IOException e) { return false; } } public static void main(String[] args) { File from = new File("/var/www/src"); File to = new File("/var/www/dest"); boolean success = moveDir(from.toPath(), to.toPath()); if (success) { System.out.println("File Moved Successfully"); } else { System.out.println("File Moved Failed"); } } } |
That’s all about moving a directory in Java.
Also See:
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)