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 › en › java › javase › 11 › docs › api › java.base › java › nio › file › Files.html
Files (Java SE 11 & JDK 11 )
January 20, 2026 - SecurityException - In the case of the default provider, and a security manager is installed, the checkRead method is invoked to check read access to the source file, the checkWrite is invoked to check write access to the target file. If a symbolic link is copied the security manager is invoked to check LinkPermission("symbolic"). public static Path move​(Path source, Path target, CopyOption...
🌐
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....
🌐
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(); }
🌐
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 - 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.
🌐
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.
🌐
Mkyong
mkyong.com › home › java › how to rename or move a file in java
How to rename or move a file in Java - Mkyong.com
July 30, 2020 - In Java, we can use the NIO `Files.move(source, target)` to rename or move a file.
🌐
GitHub
github.com › AdoptOpenJDK › openjdk-jdk11 › blob › master › src › java.base › share › classes › java › io › File.java
openjdk-jdk11/src/java.base/share/classes/java/io/File.java at master · AdoptOpenJDK/openjdk-jdk11
March 2, 2019 - * java.nio.file.Files#move move} method to move or rename a file in a · * platform independent manner. * * @param dest The new abstract pathname for the named file · * * @return <code>true</code> if and only if the renaming succeeded; * <code>false</code> otherwise ·
Author   AdoptOpenJDK
Find elsewhere
🌐
Marco Behler
marcobehler.com › guides › java-files
How To Work With Files In Java
December 9, 2020 - If you specify the StandardCopyOption.ATOMIC_MOVE option, you can move a file into a directory and be guaranteed that any process watching the directory accesses a complete file and not just a partial file. Deleting files and folders is an area, where the Java Path API falls short a tiny bit.
🌐
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("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).
🌐
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 - How do I move files with Java? How do I rename a file? How do I copy a file? How to delete a file? How to create a symbolic link with Java? 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. You can construct this object using the static method Path.of() (or, before Java 11, using Paths.get()).
🌐
EyeHunts
tutorial.eyehunts.com › home › java move file | directory method with examples code
Java Move File | Directory Method with Examples - EyeHunts
May 17, 2021 - Before reading this tutorial we suggest reading a how-to Create, Write and Delete tutorial of the java file. ... A standard move() method using NIO, where you need the pass a source file name and destination of the
🌐
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 - We can use Files.move() API to move file from one directory to another. Following is the syntax of the move method. public static Path move(Path source,Path target,CopyOption... options) throws IOException ... import java.io.IOException; import ...
🌐
Dev.java
dev.java › learn › java-io › file-system › move-copy-delete
Manipulating Files and Directories - Dev.java
January 25, 2023 - The copy(Path, OutputStream) method may be used to copy all bytes from a file to an output stream. You can move a file or directory by using the move(Path, Path, CopyOption...) method.
🌐
Java Guides
javaguides.net › 2018 › 07 › how-to-move-file-in-java.html
How to Move a File in Java
June 21, 2024 - Moving a file in Java can be accomplished using several methods, including the File class and the Files class from the NIO package. The File class provides a simple way to move files, while the Files class offers more advanced features and better support for modern file operations.
🌐
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 ...
🌐
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"); } } }