Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why does File renameTo() fails ?

 
Priyha Jootu
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Gayathri Prasad
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic