Sunday, October 14, 2007

Rename/Move Files Using java.io.File API

1. Rename a File in Place

Rename a file in the original place is trivial by using renameTo() method in java.io.File class. The following code snapshot will change a file name from “oldName.pdf” to “newName.pdf”:

String oldName = “oldName.pdf”;
String newName = “newName.pdf”;

File f = new File(oldName);
f.renameTo(newName);

2. Rename/Move a File to an Existing Directory

It is also easy to rename a file, and move the file to an existing directory. The following code will change the file name from “oldName.pdf” to “newName.pdf”, and also move the file from “C:\oldDir” to “C:\newDirsubDir” provide “C:\newDir\subDir” exists:

String oldDir = “C:\\oldDir\\”;
String oldName = “oldName.pdf”;

String newDir = “C:\\oldDir\subDir\\”;
String newName = “newName.pdf”;

File f = new File (oldDir, oldName);

f.renameTo(newDir + newName);


The last line of code will rename and also move the file to newDir , C:\newDir\subDir.

3. Rename/Move a File to an Directory that does not exist

The piece of code above won’t work if C:\newDir\subDir\ does not exist. However, we still can achieve our goal by a little more work. Here is how we do:

String oldDir = “C:\\oldDir\\”;
String oldName = “oldName.pdf”;

String newDir = “C:\\oldDir\\subDir\\”;
File pDir = new File(newDir);
pDir.mkroots();

String newName = “newName.pdf”;

File f = new File (oldDir, oldName);
f.renameTo(newDir + newName);

The last two lines of code in red will create all directories along the path if they do not exist, and then rename and move the file to the new directory.

13 comments:

Anonymous said...

java.io.File has no rename() method...perhaps you mean renameTo()?

Xiaoyun Tang said...

Yes, you are right. There is no such rename() method in java.io.File It should be renameTo().
Thanks for pointing out my mistake.

Anonymous said...

Ya It's working fine only if the source and destination are on the same drive.

Xiaoyun Tang said...

Well, on Windows, I am able to copy files over to/from a shared network drive.

Anonymous said...

The JavaDocs of renameTo() say that the ability to move a file depends on the capability of the OS. On Windows you can always move a file. On Unix you can only move if the file is on the same file system.

Anonymous said...

Hey, that was a very useful code. Thanks

Anonymous said...

I'm actually having a lot of trouble getting this to work. A simple google search reveals that this is a huge problem that bothers many people. A simple thing that doesn't work all the time!

Anonymous said...

True!

Anonymous said...

it could use like this
f.renameTo(new File(newFileName));

Anonymous said...

Unfornatly this doesn't work on Windows if the source and destonation file is in your My Documents directory. For example, try renaming a file, using renameTo, which is located in your My Documents directory. This will fail. This is due to a huge Java bug in Windows that prevents this.

Anonymous said...

Yes it is true it doesnot work for files in My Documents. If you put the files in C drive it works. Thanks

Anonymous said...

the last redline trys renameTo() with Strings. But renameTo() needs a File object as parameter!

Anonymous said...

Thanks