The renameTo() method of the File class allows us to move a file or directory into a different directory. In this phrase, we create a File object to represent the file or directory we are moving. We create another File object representing the destination directory that we are moving the file or directory into. We then call the renameTo() method on the file being moved and pass it a single File object parameter. The File object passed as a parameter is constructed using the destination directory and the original file name. If the move operation is successful, the renameTo() method will return a boolean true value. If the move fails, a boolean false is returned. When you use the renameTo() method, keep in mind that many aspects of its behavior are platform dependent. Some of these platform dependent behaviours are noted in the JavaDoc for this method. They include the following:
When using this method, you should always check the return value to make sure that the rename operation was successful. |