Somnath Mallick wrote:
That will fail if there is no \ in the absolute file path. Although on Windows this will not occur, on Linux and Mac OS it will most definitely. Just use the parent file as I have shown you. This may return null if there is no parent but the File constructor allows a null parent.
How can i make the code, do the renaming even if the file already exists?
You will have to make sure that the destination file does not exist first. For files you can simply delete() it (if you are allowed to; delete() can also return false). For folders you will need to delete all files and folders in it first.
For folders you may want to merge the folder contents though (your choice: delete, merge or fail). That involves moving (renaming) all files inside the folder you want to rename first, then remove the folder you want to rename (or from the existing to the one you want to rename).
Either way, both require a recursive call. You can find the one for deleting by doing a search around this board. The merging is a bit harder but the same principle holds.