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 OverflowYou 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.
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 IOExceptionMoves 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 directoryThrows:
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
Videos
You can try this:
srcFile.renameTo(new File("C:\\folderB\\" + srcFile.getName()));
Have you read this "http://java.about.com/od/InputOutput/a/Deleting-Copying-And-Moving-Files.htm "
Files.move(original, destination, StandardCopyOption.REPLACE_EXISTING)
move the files to destination.
If you want to move directory Use this
File dir = new File("FILEPATH");
if(dir.isDirectory()) {
File[] files = dir.listFiles();
for(int i = 0; i < files.length; i++) {
//move files[i]
}
}