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
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-program-to-rename-a-file
Java Program to Rename a File - GeeksforGeeks
July 23, 2025 - Create another object of the File class and replace the file path with the renaming path of the directory. Use renameTo() method. If rename operation successful then the function returns true.
🌐
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
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › io › File.html
File (Java Platform SE 8 )
April 21, 2026 - 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.
Find elsewhere
🌐
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.
🌐
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 ...
🌐
TutorialsPedia
tutorialspedia.com › home › java rename all files in folder
java rename all files in folder | TutorialsPedia
While developing a Java application, you may come across a scenario where you have a large number of files in a folder which don’t have required naming standards or conventions that you are in need of. In such a situation, you will wish to have a piece of code that can rename all the files…
🌐
Keymetrics
pm2.keymetrics.io › docs › usage › quick-start
PM2 - Quick Start
1 week ago - # Specify an app name --name <app_name> # Watch and Restart app when files change --watch # Set memory threshold for app reload --max-memory-restart <200MB> # Specify log file --log <log_path> # Pass extra arguments to the script -- arg1 arg2 arg3 # Delay between automatic restarts --restart-delay <delay in ms> # Prefix logs with time --time # Do not auto restart app --no-autorestart # Specify cron for forced restart --cron <cron_pattern> # Attach to application log after start --attach # Run the PM2 daemon in the foreground --no-daemon
🌐
GraphQL
graphql.org › learn › queries
Queries | GraphQL
2 weeks ago - Field aliases allow you to rename response keys, include the same field multiple times in the same query, and provide different arguments to the aliased fields
🌐
Kindle Modding
kindlemodding.org › jailbreaking › post-jailbreak › setting-up-a-hotfix
KindleModding - Setting Up A Hotfix
If you have blocked the updates using renametobin (set to Rename), make sure to revert it beforehand (by selecting Restore), or you will not be able to install the hotfix through the Settings menu · This is not necessary for some types of legacy jailbreaks, check the corresponding page ... If you see any other files on your Kindle ending in .bin, or have a similar name to update.bin.tmp.partial, you must delete them for the hotfix to work.
🌐
TutorialsPoint
tutorialspoint.com › article › rename-multiple-files-using-java
Rename multiple files using Java
July 4, 2020 - import java.io.File; import java.io.IOException; public class Demo{ public static void main(String[] argv) throws IOException{ String path_to_folder = "path\to\folder\where\multiple\files\are\present"; File my_folder = new File(path_to_folder); File[] array_file = my_folder.listFiles(); for (int i = 0; i < array_file.length; i++){ if (array_file[i].isFile()){ File my_file = new File(path_to_folder + "" + array_file[i].getName()); String long_file_name = array_file[i].getName(); String[] my_token = long_file_name.split("\s"); String new_file = my_token[1]; System.out.println(long_file_name); System.out.print(new_file); my_file.renameTo(new File(path_to_folder + "" + new_file + ".pdf")); } } } }
🌐
Kubernetes
kubernetes.io › docs › concepts › configuration › configmap
ConfigMaps | Kubernetes
November 21, 2025 - A ConfigMap is an API object used to store non-confidential data in key-value pairs. Pods can consume ConfigMaps as environment variables, command-line arguments, or as configuration files in a volume. A ConfigMap allows you to decouple environment-specific configuration from your container images, so that your applications are easily portable.
🌐
Claude Platform Docs
platform.claude.com › docs › en › agents-and-tools › tool-use › memory-tool
Memory tool - Claude Platform Docs
If the path is a directory, return a "file does not exist" error. ... Deletes the directory and all its contents recursively. The tool description tells Claude it cannot delete the /memories directory itself, so reject a delete whose path is the memory root. ... Destination already exists: Return an error (do not overwrite): "Error: The destination {new_path} already exists" Renames the directory.
🌐
Xiaocaicai
baeldung.xiaocaicai.com › java-how-to-rename-or-move-a-file
Rename or Move a File – Java –重命名或移动一个文件
June 25, 2014 - 在这个快速教程中,我们将学习在Java中重命名/移动一个文件。 · We’ll first look into using the Files and Path classes from NIO, then the Java File class, Google Guava, and finally the Apache Commons IO library.
🌐
Coderanch
coderanch.com › t › 278254 › java › File-rename-Java-API
File rename using Java API (I/O and Streams forum at Coderanch)
October 31, 2006 - You could use Runtime.exec to issue a shell command that does this, but there is no support for this in the Java class libraries. ... Presumably you know about File.renameTo(), right? It's just one file at a time, but you can always write a loop. The bigger headache is remembering to check the return value, and if it's false, figuring out why.
🌐
OpenRocket
openrocket.info › downloads.html
Downloads
OpenRocket is available as packages for Linux, macOS and Windows. A .jar file is also available, as is all of the source code. For most users, we strongly recommend using the package appropriate for your operating system. These packages contain all of the needed dependencies, including the correct version of Java.
🌐
Java Code Geeks
examples.javacodegeeks.com › home › java development › core java › io › file
Rename file/directory - Java Code Geeks
October 26, 2013 - 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, ... 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 denoted by this abstract pathname.
🌐
YouTube
youtube.com › watch
How to rename a file in java? - YouTube
#learnwithkrishnasandeep #javacodinginterviewquestions #javaexamples #javaprograms #javatutorials #javaprogramming how to rename a file in java,how to rena...
Published   December 12, 2015