Copied from (originally) http://exampledepot.8waytrips.com/egs/java.io/RenameFile.html (archived at the Wayback Machine).

// File (or directory) with old name
File file = new File("oldname");
    
// File (or directory) with new name
File file2 = new File("newname");

if (file2.exists())
   throw new java.io.IOException("file exists");
    
// Rename file (or directory)
boolean success = file.renameTo(file2);

if (!success) {
   // File was not successfully renamed
}

To append to the new file:

java.io.FileWriter out= new java.io.FileWriter(file2, true /*append=yes*/);
Answer from Pierre on Stack Overflow
๐ŸŒ
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. import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; //... Path source = Paths.get("/home/mkyong/newf...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-program-to-rename-a-file
Java Program to Rename a File - GeeksforGeeks
July 23, 2025 - ... // Java Program to rename a ... the File class // Replace the file path with path of the directory File file = new File("/home/mayur/Folder/GFG.java"); // Create an object of the File class // Replace the file path with ...
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ javaexamples โ€บ file_rename.htm
How to rename a file in Java
The above code sample will produce the following result.To test this example, first create a file 'program.txt' in 'C' drive. ... import java.io.File; public class RenameFileExample { public static void main(String[] args) { File oldfile = new File("C:/oldfile_name.txt"); File newfile = new File("C:/newfile_name.txt"); if(oldfile.renameTo(newfile)) { System.out.println("File name changed succesful"); } else { System.out.println("Rename failed"); } } }
๐ŸŒ
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"); ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-renaming-file
Java | Renaming a file - GeeksforGeeks
July 11, 2025 - import java.io.File; public class GeeksforGeeks { public static void main(String[] args) { File oldName = new File("C:\Users\Siddharth\Desktop\java.txt"); File newName = new File("C:\Users\Siddharth\Desktop\GeeksforGeeks.txt"); if ...
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ file-renameto-method-in-java-with-examples
File renameTo() method in Java with examples - GeeksforGeeks
July 11, 2025 - Below programs will illustrate the use of renameTo() function: Example 1: Try to rename the file program.txt to program1.txt ... // Java program to demonstrate // the use of File.renameTo() method import java.io.*; public class GFG { public ...
Find elsewhere
๐ŸŒ
TutorialKart
tutorialkart.com โ€บ java โ€บ java-rename-file
How to Rename File in Java?
January 10, 2023 - ... import java.io.File; /** * ... File */ public class RenameFile { public static void main(String[] args) { File originalFile = new File("files/data.txt"); File newFile = new File("files/newdata.txt"); //rename file boolean isRenameDone = originalFile.renameTo(newFile); ...
๐ŸŒ
BeginnersBook
beginnersbook.com โ€บ 2014 โ€บ 01 โ€บ how-to-rename-file-in-java-renameto-method
How to rename file in Java โ€“ renameTo() method
September 11, 2022 - It returns true if the file is renamed successfully else it returns false. It throws NullPointerException โ€“ If parameter dest is null. ... import java.io.File; public class RenameFileJavaDemo { public static void main(String[] args) { //Old File File oldfile =new File("C:\\myfile.txt"); //New File File newfile =new File("C:\\mynewfile.txt"); /*renameTo() return boolean value * It return true if rename operation is * successful */ boolean flag = oldfile.renameTo(newfile); if(flag){ System.out.println("File renamed successfully"); }else{ System.out.println("Rename operation failed"); } } }
๐ŸŒ
Quickprogrammingtips
quickprogrammingtips.com โ€บ java โ€บ how-to-rename-a-file-in-java.html
How to Rename a File in Java
* @author jj */ public class RenameFile1 { public static void main(String[] args) { // Please ensure that the following file/folder exists File f = new File("c:/temp/test1.txt"); // The following file should not exist while running the program File rF = new File("c:/temp/test2.txt"); if(f.renameTo(rF)) { System.out.println("File was successfully renamed"); } else { System.out.println("Error: Unable to rename file"); } } } Since Java 8, a new class java.nio.file.Files was introduced. This is a more reliable and flexible option than the java.io.File. The following example shows the recommended way of renaming files in Java 8.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ java-program-to-rename-a-file-or-directory
Java program to rename a file or directory
August 2, 2024 - A program that demonstrates this is given as follows ? import java.io.File; public class Demo { public static void main(String[] args) { try { File file1 = new File("demo1.txt"); File file2 = new File("demo2.txt"); file1.createNewFile(); file2.createNewFile(); boolean flag = file1.renameTo(file2); ...
๐ŸŒ
Unibz
inf.unibz.it โ€บ ~calvanese โ€บ teaching โ€บ ip โ€บ lecture-notes โ€บ uni09 โ€บ node12.html
Renaming and deleting a file in Java
File f1 = new File("oldname.txt"); File f2 = new File("newname.txt"); boolean b = f1.renameTo(f2); // if b is true, then the file has been renamed successfully
๐ŸŒ
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.
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ java โ€บ io โ€บ file_renameto.htm
Java - File renameTo(File dest) method
Then we're creating a File Object using a file path which is present in the given location and a new File which is not present. Using renameTo() method, we're trying to rename the first time and getting the result in boolean variable. Then we're printing the status of directory being created or not. package com.tutorialspoint; import java.io.File; public class FileDemo { public static void main(String[] args) { File f = null; File f1 = null; boolean bool = false; try { // create new File objects f = new File("F:/Test2/test.txt"); f1 = new File("F:/Test2/testABC.txt"); // rename file bool = f.renameTo(f1); // print System.out.print("File renamed?
๐ŸŒ
Java Code Geeks
examples.javacodegeeks.com โ€บ home โ€บ java development โ€บ core java โ€บ io โ€บ file
Rename file/directory - Java Code Geeks
October 26, 2013 - package com.javacodegeeks.snippets.core; import java.io.File; public class RenameFileDirectory { public static void main(String[] args) { File file = new File("C://file.txt"); File newFile = new File("C://new_file.txt"); // Renames the file ...
๐ŸŒ
CalliCoder
callicoder.com โ€บ java-move-file
How to move or rename a File or Directory in Java | CalliCoder
February 18, 2022 - To move a directory with along with its contents, you would need to recursively call move for the subdirectories and files. We will look at this in another article. Here is an example of moving or renaming a directory: import java.io.IOException; import java.nio.file.*; public class MoveDirectoryExample { public static void main(String[] args) { Path sourceFilePath = Paths.get("/Users/callicoder/Desktop/media"); Path targetFilePath = Paths.get("/Users/callicoder/Desktop/new-media"); try { Files.move(sourceFilePath, targetFilePath); } catch (FileAlreadyExistsException ex) { System.out.println("Target file already exists"); } catch (IOException ex) { System.out.format("I/O error: %s%n", ex); } } }
๐ŸŒ
Java Guides
javaguides.net โ€บ 2018 โ€บ 07 โ€บ how-to-rename-file-in-java.html
How to Rename a File in Java
June 21, 2024 - Java offers multiple ways to rename a file depending on the version and specific requirements of your application. The traditional approach involves using the File class from the java.io package, while the modern approach utilizes the Files class from the java.nio.file package introduced in Java 7.
๐ŸŒ
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 - You can use Java NIOโ€™s Files.move() method to copy or rename a file or directory. package com.example.java.programming.file;import java.io.IOException; import java.nio.file.FileAlreadyExistsException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths;/** * @author code.factory * */ public class MoveFileExample { public static void main(String...