myFile.renameTo(new File("/the/new/place/newName.file"));

File#renameTo does that (it can not only rename, but also move between directories, at least on the same file system).

Renames the file denoted by this abstract pathname.

Many aspects of the behavior of this method are inherently platform-dependent: The rename operation might not be able to move a file from one filesystem to another, it might not be atomic, and it might not succeed if a file with the destination abstract pathname already exists. The return value should always be checked to make sure that the rename operation was successful.

If you need a more comprehensive solution (such as wanting to move the file between disks), look at Apache Commons FileUtils#moveFile

Answer from Thilo on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › tutorial › essential › io › move.html
Moving a File or Directory (The Java™ Tutorials > Essential Java Classes > Basic I/O)
The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available. See Dev.java for updated tutorials taking advantage of the latest releases.
🌐
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 `Files.move` and `JSch` move a file to another directory in the same file drive or remote server.
🌐
Javabrahman
javabrahman.com › corejava › copy-move-file-java-one-location-another-examples
How to copy or move a file in Java from one location ...
This quick code tip shows how to use Java NIO API to copy or move a file from one location in the file system to another. Java 8 code to COPY & MOVE files · package com.javabrahman.corejava; import java.io.IOException; import java.nio.file.FileSystem; import java.nio.file.FileSystems; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardCopyOption; public class CopyMoveFile { public static void main(String args[]) { FileSystem fileSys = FileSystems.getDefault(); Path srcPath = fileSys.getPath("c:\\src_folder\\file1.txt"); Path destPath = fileSys.getPath("c:\\des
🌐
TutorialsPoint
tutorialspoint.com › article › moving-a-file-from-one-directory-to-another-using-java
Moving a file from one directory to another using Java
June 25, 2020 - import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class Tester { public static void main(String[] args) { //move file from D:/temp/test.txt to D:/temp1/test.txt //make sure that temp1 folder exists moveFile("D:/temp/test.txt", "D:/temp1/test.txt"); } private static void moveFile(String src, String dest ) { Path result = null; try { result = Files.move(Paths.get(src), Paths.get(dest)); } catch (IOException e) { System.out.println("Exception while moving file: " + e.getMessage()); } if(result != null) { System.out.println("File moved successfully."); }else{ System.out.println("File movement failed."); } } }
🌐
Java67
java67.com › 2016 › 09 › how-to-copy-file-from-one-location-to-another-in-java.html
3 ways to Copy a File From One Directory to Another in Java, Examples | Java67
1) The Files.copy() should be a standard way to copy a file from one folder to another if you are working in Java 7 and Java 8. 2) For copying files in Java 6, you can either write your own code using FileChannel, FileInputStream or can leverage ...
🌐
Java Guides
javaguides.net › 2018 › 07 › how-to-move-file-in-java.html
How to Move a File in Java
June 21, 2024 - ▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube · Moving a file in Java involves relocating a file from one directory to another or renaming it within the same directory. Java provides multiple ways to accomplish this task using the File class from the java.io package and the Files class from the java.nio.file package.
🌐
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 - // 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"); } } }
Find elsewhere
🌐
LinkedIn
linkedin.com › pulse › copy-list-files-from-one-folder-another-java-rishal-dev-singh
Copy List of files from one folder to another in Java
July 1, 2021 - * * @author Rishal * */ public class MoveFiles { public static void main(String[] args) throws IOException { // Path of the file which is having all the names of the files which we // want to move into another folder. String list = "D:/CodingPlayGround/listOfFiles.txt"; List listOfFileNamesToBeMoved = Files.readAllLines(Paths.get(list), Charset.forName("UTF-8")); // Input object to get input from the user BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); // Reading source and destination from the user System.out.println("Enter Source folder path:"); String sourceFolder = br.readLine(); System.out.println("Enter Target folder path:"); String targetFolder = br.readLine(); // Creating file object with source folder path passed as input by the // user.
🌐
Medium
medium.com › @AlexanderObregon › javas-files-move-method-explained-7dee1287fa92
Java’s Files.move() Method Explained | Medium
September 14, 2024 - Learn how to use Java's Files.move() method for moving, renaming, and organizing files, with examples of atomic moves and overwriting files efficiently.
🌐
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 - moveDirectoryToDirectory(File src, File destDir, boolean createDestDir) : Moves a directory to another directory and takes an option to create the new directory or not.
🌐
Baeldung
baeldung.com › home › java › java io › java – rename or move a file
Rename or Move a File in Java | Baeldung
January 5, 2024 - How to rename or move a file in Java, using JDK6, JDK7, Guava or Apache Commons.
🌐
JavaBeat
javabeat.net › home › how to move a file using java
How to Move a File Using Java
May 6, 2024 - Let’s learn how the renameTo() method works using the below coding example: import java.io.*; import java.nio.file.*; public class MoveFileJava { public static void main(String[] args) throws FileSystemException { File sourceFile = new ...
🌐
Attacomsian
attacomsian.com › blog › java-move-file-to-another-directory
How to move a file to another directory in Java
December 15, 2019 - try { // source & destination files Path src = Paths.get("dir1/input.txt"); Path target = Paths.get("dir2/input.txt"); // move file fron one location to another Files.move(src, target, StandardCopyOption.REPLACE_EXISTING); } catch (IOException ex) { ex.printStackTrace(); } 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:
🌐
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("CoverPhoto.png"); Path newdir = Paths.get("D:/Photo"); Files.move(source, newdir.resolve(source.getFileName()), StandardCopyOption.REPLACE_EXISTING);And you can move an empty directory to another location, for example: Path source = Paths.get("test"); Path newdir = Paths.get("D:/Photo"); Files.move(source, newdir.resolve(source.getFileName()));NOTE: You can move only empty directory and replace existing directory if it is also empty. In either a directory is not empty, a DirectoryNotEmptyException is thrown. ... Nam Ha Minh is certified Java programmer (SCJP and SCWCD).
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