How to rename file in Java

Java comes with renameTo() method to rename a file. However , this method is really platform-dependent: you may successfully rename a file in *nix but failed in Windows. So, the return value (true if the file rename successful, false if failed) should always be checked to make sure the file is rename successful.

File.renameTo() Example


package com.mkyong.file;

import java.io.File;

public class RenameFileExample 
{
    public static void main(String[] args)
    {	
    	
		File oldfile =new File("oldfile.txt");
		File newfile =new File("newfile.txt");
		
		if(oldfile.renameTo(newfile)){
			System.out.println("Rename succesful");
		}else{
			System.out.println("Rename failed");
		}
    	
    }
}

Reference

  1. File renameTo() documentation

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
DingThree
Guest
DingThree

I have space in my file name, because of that i am not able to rename my file.could anyone please help on that?

jonathan
Guest
jonathan

I use, Batch Rename Files Tool. You can easily found hier BatchRenameFiles.org that allows you to quickly rename all the files in a specified directory.

Thein Than Aung
Guest
Thein Than Aung

//How does not work in window?Please tell me another way.Thank you so much.

import java.io.File;

public class RenameFileExample
{
public static void main(String[] args)
{

File oldfile =new File(“oldfile.txt”);
File newfile =new File(“newfile.txt”);

if(oldfile.renameTo(newfile)){
System.out.println(“Rename succesful”);
}else{
System.out.println(“Rename failed”);
}

}
}

Saikat
Guest
Saikat

sometimes it doesn’t work if the file in question is not accessible without admin permissions.

i.e if in windows explorer on trying to rename that file results in “permission from administrator” dialog

listened
Guest
listened

I rename all very well,in windows 7 x64.

listened
Guest
listened

That is helpful to me.Thank you.

jo
Guest
jo

I have two files in the directory, the first one fails but the second is successful when using renameTo. Why is it like that?

Anand
Guest
Anand

Hi you will get complete java code for all file operations here – http://javadiscover.blogspot.com/2013/03/how-to-create-writeappend-read-delete.html

Sudhir
Guest
Sudhir

sometimes renameTo doesn’t work in windows.
I am still trying to find the the answer.
Can you please help me.

Thanks,
Sudhir

Harshil Sharma
Guest
Harshil Sharma

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.

Karren Omoto
Guest
Karren Omoto

Someone I work with visits your site frequently and recommended it to me to read also. The writing style is great and the content is relevant. Thanks for the insight you provide the readers!