You can simply move directory by using

import static java.nio.file.StandardCopyOption.*;

Files.move(new File("C:\\projects\\test").toPath(), new File("C:\\projects\\dirTest").toPath(), StandardCopyOption.REPLACE_EXISTING);

Change source and destination path

Refer here to get more details

Also Note from API

 When invoked to move a
     * directory that is not empty then the directory is moved if it does not
     * require moving the entries in the directory.  For example, renaming a
     * directory on the same {@link FileStore} will usually not require moving
     * the entries in the directory. When moving a directory requires that its
     * entries be moved then this method fails (by throwing an {@code
     * IOException}). To move a <i>file tree</i> may involve copying rather
     * than moving directories and this can be done using the {@link
     * #copy copy} method in conjunction with the {@link
     * #walkFileTree Files.walkFileTree} utility method

If you try to move the file in the same partition , the above code is sufficient ( it can move directory even it has entries). if not ( instead of move) you need to use recursive as other answer mentioned.

Answer from Mani on Stack Overflow
Top answer
1 of 8
22

You can simply move directory by using

import static java.nio.file.StandardCopyOption.*;

Files.move(new File("C:\\projects\\test").toPath(), new File("C:\\projects\\dirTest").toPath(), StandardCopyOption.REPLACE_EXISTING);

Change source and destination path

Refer here to get more details

Also Note from API

 When invoked to move a
     * directory that is not empty then the directory is moved if it does not
     * require moving the entries in the directory.  For example, renaming a
     * directory on the same {@link FileStore} will usually not require moving
     * the entries in the directory. When moving a directory requires that its
     * entries be moved then this method fails (by throwing an {@code
     * IOException}). To move a <i>file tree</i> may involve copying rather
     * than moving directories and this can be done using the {@link
     * #copy copy} method in conjunction with the {@link
     * #walkFileTree Files.walkFileTree} utility method

If you try to move the file in the same partition , the above code is sufficient ( it can move directory even it has entries). if not ( instead of move) you need to use recursive as other answer mentioned.

2 of 8
10

If you have imported Apache Commons already anyways:

FileUtils.moveDirectory(oldDir, newDir);

Note that newDir must not exist beforehand. From the javadocs:

public static void moveDirectory(File srcDir,
                                 File destDir)
                      throws IOException

Moves a directory. When the destination directory is on another file system, do a "copy and delete".

Parameters:
srcDir - the directory to be moved
destDir - the destination directory

Throws:
NullPointerException - if source or destination is null
FileExistsException - if the destination directory exists
IOException - if source or destination is invalid
IOException - if an IO error occurs moving the file

🌐
Oracle
docs.oracle.com › javase › tutorial › essential › io › move.html
Moving a File or Directory (The Java™ Tutorials > Essential Java Classes > Basic I/O)
See Java Language Changes for a ... removed or deprecated options for all JDK releases. You can move a file or directory by using the move(Path, Path, CopyOption...) method....
🌐
Medium
medium.com › @AlexanderObregon › javas-files-move-method-explained-7dee1287fa92
Java’s Files.move() Method Explained | Medium
September 14, 2024 - The Files.move() method is a powerful and flexible tool used for moving or renaming files and directories in Java applications.
🌐
Dev.java
dev.java › learn › java-io › file-system › move-copy-delete
Manipulating Files and Directories - Dev.java
January 25, 2023 - You can move a file or directory by using the move(Path, Path, CopyOption...) method.
🌐
CodeJava
codejava.net › java-se › file-io › how-to-rename-move-file-or-directory-in-java
How to rename/move file or directory in Java
Path source = Paths.get("Notes.txt"); Files.move(source, source.resolveSibling("Keynotes.txt"));If the target file exists, this method throws java.nio.file.FileAlreadyExistsException. You can specify the option to replace the existing file like this: Path source = Paths.get("Notes.txt"); Files.move(source, source.resolveSibling("Keynotes.txt"), StandardCopyOption.REPLACE_EXISTING);The following example renames a directory to a new one:
🌐
Kodejava
kodejava.org › how-do-i-move-directory-to-another-directory-with-its-entire-contents
How do I move directory to another directory with its entire contents? - Learn Java by Examples
We can use the FileUtils.moveDirectory() method to simplify the process. package org.kodejava.commons.io; import org.apache.commons.io.FileUtils; import java.io.File; import java.io.IOException; public class DirectoryMove { public static void ...
🌐
CalliCoder
callicoder.com › java-move-file
How to move or rename a File or Directory in Java | CalliCoder
February 18, 2022 - You can use Java NIO’s Files.move() method to copy or rename a file or directory.
🌐
HappyCoders.eu
happycoders.eu › java › how-to-list-directory-contents-move-copy-delete-files
How to List, Move, Copy, and Delete Files (Java Tutorial)
November 29, 2024 - The article answers all questions using the NIO.2 File API, introduced in Java 7 with JSR 203. For the following directory operations, you need a Path-object representing the directory.
Find elsewhere
🌐
Mkyong
mkyong.com › home › java › how to move file to another directory in java
How to move file to another directory in Java - Mkyong.com
August 4, 2020 - This article shows how to move a file to another directory in the same file drive or remote server. Files.move – Move file in local system. ... This Java example uses NIO Files.move to move a file from to another directory in the same local drive.
🌐
How to do in Java
howtodoinjava.com › home › i/o › rename or move a file or directory in java
Rename or Move a File or Directory in Java
November 14, 2022 - This Files.move() method moves a file from one path to another. This is applicable to renaming and moving, both operations. We should be careful that the destination path must be the target path for the file itself; not just the new name for ...
🌐
Baeldung
baeldung.com › home › java › java io › copy a directory in java
Copy a Directory in Java | Baeldung
January 8, 2024 - However, if we want to stay compatible with older Java versions, we can copy a directory using recursion and java.io.File features:
🌐
Baeldung
baeldung.com › home › java › java io › java – rename or move a file
Rename or Move a File in Java | Baeldung
January 5, 2024 - Examples of how to get the size of a file in Java. ... In the examples, we’ll use the following setup, which consists of 2 constants for the source and destination file name and a clean-up step to be able to run the tests multiple times: private final String FILE_TO_MOVE = "src/test/resources/originalFileToMove.txt"; private final String TARGET_FILE = "src/test/resources/targetFileToMove.txt"; @BeforeEach public void createFileToMove() throws IOException { File fileToMove = new File(FILE_TO_MOVE); fileToMove.createNewFile(); } @AfterEach public void cleanUpFiles() { File targetFile = new File(TARGET_FILE); targetFile.delete(); }
🌐
Attacomsian
attacomsian.com › blog › java-move-file-to-another-directory
How to move a file to another directory in Java
December 15, 2019 - In older Java versions (Java 6 and below), you can call the renameTo() method on a File object to move file from one directory to another directory as shown below:
🌐
GeeksforGeeks
geeksforgeeks.org › java › moving-file-one-directory-another-using-java
Moving a file from one directory to another using Java - GeeksforGeeks
October 21, 2021 - public static Path move(Path source, Path target, CopyOption..options) throws IOException Parameters: source - the path to the file to move target - the path to the target file (may be associated with a different provider to the source path) options - options specifying how the move should be done Returns: the path to the target file ... // Java program to illustrate renaming and // moving a file permanently to a new location import java.io.*; import java.nio.file.Files; import java.nio.file.*; public class Test { public static void main(String[] args) throws IOException { Path temp = Files.move (Paths.get("C:\\Users\\Mayank\\Desktop\\44.txt"), Paths.get("C:\\Users\\Mayank\\Desktop\\dest\\445.txt")); if(temp != null) { System.out.println("File renamed and moved successfully"); } else { System.out.println("Failed to move the file"); } } }
🌐
javaspring
javaspring.net › blog › java-move-directory-containing-files-and-directories-to-new-path
Java: How to Move a Directory with Files and Subdirectories to a New Path – API Solutions vs Recursive Copy
In this blog, we’ll explore two primary strategies for moving directories: Using Java’s Built-in APIs (e.g., Files.move() from NIO.2 or legacy File.renameTo()).
🌐
Niraj Sonawane
nirajsonawane.github.io › 2018 › 05 › 30 › java-8-File-Operations-Copy-Delete-Move
Java 8 File Operations - Copy,Delete,Move | Niraj Sonawane
June 10, 2018 - Copy Non empty Directory Directories can be copied. However, files inside the directory are not copied, so the new directory is empty even when the original directory contains files. ... You can move a file or directory by using the move(Path, Path, CopyOption...) method.
🌐
Java Code Geeks
javacodegeeks.com › home › core java
Java 7: Copy and Move Files and Directories - Java Code Geeks
October 21, 2012 - The Files class consists of static methods that use Path objects to work with files and directories. While there are over 50 methods in the Files class, at this point we are only going to discuss the copy and move methods. To copy one file to another you would use the (any guesses on the name?) Files.copy method – copy(Path source, Path target, CopyOption… options) very concise and no anonymous inner classes, are we sure it’s Java?.
🌐
EyeHunts
tutorial.eyehunts.com › home › java move file | directory method with examples code
Java Move File | Directory Method with Examples - EyeHunts
May 17, 2021 - A standard move() method using NIO, where you need the pass a source file name and destination of the · file. You can change the name of the file also and other options are optional. Need to import a java.nio package.