How to rename a file or folder in Java

To rename a file or folder in Java you can use the following snippet.

Sample Java

File source = new File("oldDirectoryOrFilePath");  
File target = new File("newDirectoryOrFilePath"); 
boolean done = source.renameTo(target);  
if (!done) {  
    // renaming failed  
}  

Please Share us:
Please rate this snippet

3 thoughts on “How to rename a file or folder in Java

Leave a Reply