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
🌐
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 - Learn to rename a file or directory at a specified path or move to a new directory in Java using IO, NIO, Guava and Commons IO classes.
🌐
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("Cover... if it is also empty. In either a directory is not empty, a DirectoryNotEmptyException is thrown. File.renameTo() method Javadoc ·...
🌐
Medium
34codefactory.medium.com › java-how-to-move-or-rename-a-file-or-directory-code-factory-d13ce06f78ec
Java — How to move or rename a File or Directory | Code Factory | by Code Factory | Medium
July 21, 2020 - If you want to rename a file, then ...sourceFilePath, targetFilePath); You can move or rename an empty directory using the Files.move() method....
🌐
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....
🌐
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 - Path source = Paths.get("/home/mkyong/hello.txt"); try{ // rename a file in the same directory Files.move(source, source.resolveSibling("newName.txt")); } catch (IOException e) { e.printStackTrace(); } 1.2 If the target file exists, the Files.move throws FileAlreadyExistsException. java.nio.file.FileAlreadyExistsException: /home/mkyong/newName.txt at java.base/sun.nio.fs.UnixCopyFile.move(UnixCopyFile.java:450) at java.base/sun.nio.fs.UnixFileSystemProvider.move(UnixFileSystemProvider.java:267) at java.base/java.nio.file.Files.move(Files.java:1421) at com.mkyong.io.file.FileRename.main(FileRename.java:26)
🌐
YouTube
youtube.com › watch
How To Rename A File Or Move A File To Another Directory In Java? | Java File | Java IO - YouTube
In this Java programming tutorial, we will cover two essential file operations: renaming a file and moving a file to another directory using Java. These oper...
Published   October 24, 2016
🌐
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. import java.io.IOException; import java.nio.file.*; public class MoveFileExample { public static void main(String[] args) { Path sourceFilePath = Paths.get(...
🌐
Codez Up
codezup.com › home › rename move files from one folder to another – java
Rename Move Files from One Folder To Another - Java
May 28, 2020 - Example: If the current name of files in D:\filesdata is A.txt then it is renamed to movedfilesA.txt in C:\filesdatainc. ... Please note you have to create empty directory C:\filesdatainc manually. package com.renameandmovefiles.anotherfolder; import java.io.File; import java.io.IOException; public class MoveAndRenameDirectory { public static void main(String[] args) throws IOException { // current folder name or src folder File folder = new File("D:\\filesdata"); MoveAndRenameDirectory listFiles = new MoveAndRenameDirectory(); listFiles.listAllFiles(folder); folder.delete(); } public void listAllFiles(File folder) throws IOException { File[] fileNames = folder.listFiles(); for (File file : fileNames) { System.out.println(file); // destination directory with renamed file name file.renameTo(new File("C:\\filesdatainc\\movedfile" + file.getName())); } } }
Find elsewhere
🌐
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.
🌐
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 - try { // source & destination files ... 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 - The first method utilizes Files package for moving while the other method first copies the file to destination and then deletes the original copy from the source. Using Files.Path move() method: Renaming and moving the file permanently to a ...
🌐
Delft Stack
delftstack.com › home › howto › java › different ways to move a file in java
How to Move File From Current Directory to a New Directory in Java | Delft Stack
February 2, 2024 - Use the renameTo() Method to Move a File From the Current Directory to a New Directory · This tutorial presents different ways to move a file in Java. We will learn about the two methods to move a file from one directory to another directory ...
🌐
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.
🌐
Java Guides
javaguides.net › 2019 › 07 › java-move-or-rename-file-with-files-move-api.html
Move or Rename File with Files move() Method in Java
June 21, 2024 - The code handles exceptions to ensure that informative messages are displayed if an error occurs. The Files.move() method in Java provides a simple and efficient way to move or rename files in the file system.
🌐
CodeSpeedy
codespeedy.com › home › moving a file from one directory to another using java
Moving a file from One Directory to Another using Java - CodeSpeedy
June 8, 2019 - Now what we will do to move the file in Java is, provide the path in method parameter with the same name, which will result in moving the file from our original directory to our destination directory with the same name.