November 2006
Intermediate to advanced
224 pages
3h 29m
English
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 ...
Read now
Unlock full access