File renameTo() method in Java with examples Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report The renameTo() method is a part of File class. The renameTo() function is used to rename the abstract path name of a File to a given path name. The function returns true if the file is renamed else returns false Function Signature: public boolean renameTo(File destination) Syntax: file.renameTo(File destination) Parameters: The function requires File object destination as parameter, the new abstract path name of the present file. Return Value: The function returns boolean data type. The function returns true the file is renamed else returns false Exception: This method throws following exceptions: Security Exception if the method does not allow write operation of the abstract pathnames. NullPointerException if the destination filename is null. Below programs will illustrate the use of renameTo() function: Example 1: Try to rename the file program.txt to program1.txt Java // Java program to demonstrate // the use of File.renameTo() method import java.io.*; public class GFG { public static void main(String args[]) { // create an abstract pathname (File object) File f = new File("F:\\program.txt"); // create the destination file object File dest = new File("F:\\program1.txt"); // check if the file can be renamed // to the abstract path name if (f.renameTo(dest)) { // display that the file is renamed // to the abstract path name System.out.println("File is renamed"); } else { // display that the file cannot be renamed // to the abstract path name System.out.println("File cannot be renamed"); } } } Output: File is renamed Example 2: Try to rename "program1.txt" to "prog.txt", "prog.txt" is a existing file in the f: drive . Java // Java program to demonstrate // the use of File.renameTo() method import java.io.*; public class GFG { public static void main(String args[]) { // create an abstract pathname (File object) File f = new File("F:\\program1.txt"); // create the destination file object File dest = new File("F:\\prog.txt"); // check if the file can be renamed // to the abstract path name if (f.renameTo(dest)) { // display that the file is renamed // to the abstract path name System.out.println("File is renamed"); } else { // display that the file cannot be renamed // to the abstract path name System.out.println("File cannot be renamed"); } } } Output: File cannot be renamed The programs might not run in an online IDE. please use an offline IDE and set the path of the file Comment More infoAdvertise with us Next Article FileStore name() method in Java with Examples A andrew1234 Follow Improve Article Tags : Java Java-Functions Java-IO package Java-File Class Practice Tags : Java Similar Reads FileStore name() method in Java with Examples The name() method of a FileStore class is used to return the name of this file store as a String. The format of the name is typically the name of the storage pool or volume. The string returned by this method may differ from the string returned by the toString() method. Syntax: public abstract Strin 2 min read File mkdir() method in Java with examples The mkdir() method is a part of File class. The mkdir() function is used to create a new directory denoted by the abstract pathname. The function returns true if directory is created else returns false. Function Signature: public boolean mkdir() Syntax: file.mkdir() Parameters: This method do not ac 2 min read File mkdir() method in Java with examples The mkdir() method is a part of File class. The mkdir() function is used to create a new directory denoted by the abstract pathname. The function returns true if directory is created else returns false. Function Signature: public boolean mkdir() Syntax: file.mkdir() Parameters: This method do not ac 2 min read Path getFileName() method in Java with Examples The Path interface was added to Java NIO in Java 7. The Path interface is located in the java.nio.file package, so the fully qualified name of the Java Path interface is java.nio.file.Path. A Java Path instance represents a path in the file system. A path can use to locate either a file or a directo 2 min read Field toString() method in Java with Examples The toString() method of java.lang.reflect.Field is used to get a string describing this Field. The format of the returned string is the access modifiers for the field, if any, followed by the field type, followed by a space, followed by the fully-qualified name of the class declaring the field, fol 3 min read Files copy() Method in Java with Examples The copy() method of java.nio.file.Files Class is used to copy bytes from a file to I/O streams or from I/O streams to a file. I/O Stream means an input source or output destination representing different types of sources e.g. disk files. Methods: Based on the type of arguments passed, the Files cla 5 min read Like