In this section, you will see that how a file or directory is renamed. This program illustrates you the procedure of doing so. Through this program you can easily rename any type of the file or directory. If you mention the directory (path) with the new file or directory name to rename the old file or directory which has to be renamed then the file or directory will be moved from it's own directory to the specified new directory otherwise file or directory will be moved from it's own directory to the default directory where your program has been running. This program also tells you about the availability of the specified file or directory which has to be renamed.
The method renameTo(new_file_instance) can be used to rename the appropriate file or directory. Method renameTo() renames the specified file or directory and returns a boolean value (true or false). Syntax of the renameTo() method used in this program :
oldfile : oldfile is the created instance of the
File class for holding the specified file or
directory name which has to be renamed in the file format.
newfile : newfile is also the instance of File class, which is the new name of file or directory
There are no any typical logic has been used in this program. Simply this program first reads the file or directory name which has to be renamed and then check whether the specified file or directory exists or not. If the specified file or directory exists then the program renames that file or directory into the name specified as parameter to the renameTo() method.
Here is the video tutorial of: "How to Rename the File or Directory in Java?"
Here is the code of the program :
Posted on: February 22, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles