Java Program to Rename a File Last Updated : 23 Jul, 2025 Comments Improve Suggest changes Like Article Like Report Changing the name of the file is known as Renaming the file. Rename operation is possible using renameTo() method belongs to the File class in java. A. renameTo() Method The renameTo() method is used to rename the abstract pathname of a file to a given pathname. The method returns a boolean value i.e. returns true if the file is renamed else returns false. Approach Create an object of the File class and replace the file path with the path of the directory.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.Else returns false. Below is the implementation of the above approach. Java // Java Program to rename a file import java.io.File; public class GFG { public static void main(String[] args) { // Create an object of 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 path of the directory File rename = new File("/home/mayur/Folder/HelloWorld.java"); // store the return value of renameTo() method in // flag boolean flag = file.renameTo(rename); // if renameTo() return true then if block is // executed if (flag == true) { System.out.println("File Successfully Rename"); } // if renameTo() return false then else block is // executed else { System.out.println("Operation Failed"); } } } Output: File Successfully Rename Before Program Execution After Program Execution B. move() Method Rename of file can be done using move the contents of the first file to a new file and deleting the previous file. Java is handling this operation using resolveSibiling method. It is used to resolve the given path against this path’s parent path Java // Java Program to rename a file import java.nio.file.*; import java.io.IOException; public class GFG { public static void main(String[] args) throws IOException { Path oldFile = Paths.get("/home/mayur/Folder/GFG.java"); try { Files.move(oldFile, oldFile.resolveSibling( "HelloWorld.java")); System.out.println("File Successfully Rename"); } catch (IOException e) { System.out.println("operation failed"); } } } Output: File Successfully Rename Before Program Execution After Program Execution Comment More infoAdvertise with us M mukulsomukesh Follow Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 Java-Files +1 More Practice Tags : Java Explore BasicsIntroduction to Java4 min readJava Programming Basics9 min readJava Methods7 min readAccess Modifiers in Java6 min readArrays in Java9 min readJava Strings8 min readRegular Expressions in Java7 min readOOP & InterfacesClasses and Objects in Java10 min readJava Constructors10 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface11 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java6 min readIterator in Java5 min readJava Comparator Interface6 min readException HandlingJava Exception Handling8 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java10 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial15+ min readJava Networking15+ min readJDBC Tutorial12 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples8 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions7 min readJava Quiz | Level Up Your Java Skills1 min readTop 50 Java Project Ideas For Beginners and Advanced [Update 2025]15+ min read Like