aspose file tools*
The moose likes Java in General and the fly likes Why does File renameTo() fails ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Why does File renameTo() fails ?" Watch "Why does File renameTo() fails ?" New topic
Author

Why does File renameTo() fails ?

Priyha Jootu
Ranch Hand

Joined: Sep 13, 2001
Posts: 47
Hi guys,
Below is the code where i read a RandomAccessFile (RAF) and search for a string and insert a line at the particular location. I do this by creating 2 RAF's. The new RAF ( arrayfile2, in the code below)is the resulting file with the inserted string, I am trying to rename the arrayfile2 to arrayfile1 , but the rename always return false. I am not sure why rename is not successful.
I checked for the permissions of the file, full permission is there. I closed the files before renaming.


Please let me know whats wrong with the above code. I have no clue why renameTo returns false.
Thanks,
Priyha
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
Probably because the original arrayfile1 still exists. The system won't let you have two files with the same name. Execute arrayfile1.delete() first (and check the return value to make sure it worked) then try the rename.


"I'm not back." - Bill Harding, Twister
Gayathri Prasad
Ranch Hand

Joined: Jun 25, 2003
Posts: 116
HI,
I support JIM, the renameTo() method is basically platform depenedent (on File system). But what Jim said is right. If a file already exists with the name then renameTo() returns false.
Cheers,
Gaya3
Priyha Jootu
Ranch Hand

Joined: Sep 13, 2001
Posts: 47
yes Jim , you are absolutely right, it worked now. I dont know why it didn't come to my mind before, i really appreciate your help.
Thanks a lot,
Priyha.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I really wish they had written renameTo() and delete() to throw an exception when they fail, with a message explaining why. It would save people a lot of grief...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why does File renameTo() fails ?