This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Changing File Name Not Working.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Changing File Name Not Working...." Watch "Changing File Name Not Working...." New topic
Author

Changing File Name Not Working....

Somnath Mallick
Ranch Hand

Joined: Mar 04, 2009
Posts: 477
Hi Everyone,

I am trying to write a piece of code which changes the file name and removes all the white spaces in it and deletes "-" if it comes for than once in the file name. I have done all the coding part. But the thing when i enter a folder name, none of the file names in the folder change.

Here is my code:


Could you tell me that what i am doing is right or not?
Kia Phia Ben
Ranch Hand

Joined: Dec 16, 2005
Posts: 48
Hi Somnath Mallick
I think when you input file is folder name. The first you using method isDirectory() and check folder or file, after that, you list all file in folder and change it.

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

First of all, you know that:
a) you are only removing the very last dash, not all but the first
b) you don't need to provide the substring end if you want the remainder; the substring(int) method already takes everything from the given index to the end
c) you can combine the two substring method calls that are used for tempFileName2; simply use lastIndexOf... + 1 in the first call:

There is one flaw in your renaming mechanism, and that is related to the file you rename to. "new File(newFileName)" will create a File object that points to the current directory. That may be different from the directory the file was in. You should use the file's parent:
That said, you should also check whether or not renameTo returns false. It will do so if the renaming fails, among others because the file you want to rename to already exists.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Somnath Mallick
Ranch Hand

Joined: Mar 04, 2009
Posts: 477
Kia Phia Ben wrote:Hi Somnath Mallick
I think when you input file is folder name. The first you using method isDirectory() and check folder or file, after that, you list all file in folder and change it.

I am doing the check in a different class. This is just part of a larger code!



I modified the code and this seems to work!


That said, you should also check whether or not renameTo returns false. It will do so if the renaming fails, among others because the file you want to rename to already exists.


How can i make the code, do the renaming even if the file already exists?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

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.
Somnath Mallick
Ranch Hand

Joined: Mar 04, 2009
Posts: 477
Thanks Rob for the suggestions. I have implemented your code change!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Changing File Name Not Working....
 
Similar Threads
changing filename after uploading image
how to change content type from multipart/form-data to text/html
ClassNotFoundException error
file uploading problem ( greater than 2MB)
Not able to insert image more than 4K in Database