O'Reilly logo

Java™ Phrasebook by Timothy Fisher

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Renaming a File or Directory

File f = new File("myfile.txt");
											File newFile = new File("newname.txt");
											boolean result = f.renameTo(newFile);

In this phrase, we rename a file from myfile.txt to newname.txt. To accomplish this task, we have to create two File objects. The first File object is constructed with the current name of the file. Next, we create a new File object using the name we want to rename the file. We then call the renameTo() method on the original File object and pass in the File object that specifies the new filename. The renameTo() method will return a boolean value of true if the rename operation is successful and false if it fails for any reason.

This technique can also be used to rename a directory. The code for renaming a ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required