Last Updated:

How to rename a file in Java?

Rename a file in Java. Theory

The method can be used to rename or move a file in Java. This method returns if the renaming of the files is successful, otherwise it returns . Some operations are platform-specific, for example, renaming may fail if you move a file from one file system to another or if a file with the same name already exists at the destination.

renameTo(File dest)truefalse

Rename a file in Java. Practice

Here is an example program showing different ways to rename a file in Java.

An important point! We should always check the return value of the method in order to make sure that the file was successfully renamed. It all depends on the platform and does not throw an exception if the renaming fails.