0

I have this code here that has been working for months now but suddenly decided to start throwing the exception in the if statement. Any ideas? Any other information that you need? The oldCSV.csv and newCSV.csv both exist in the directory already.

    String filePath = "\\example\example\oldCSV.csv"
    File oldFile = new File(filePath)           // oldFile = "\\example\example\oldCSV.csv"
    String parent = oldFile.getParent()             // parent = "\\example\example"
    File newFile = new File(parent, "newCSV.csv") // newFile = "\\example\example"
    if(!oldFile.renameTo(newFile)) {
        throw new Exception("Rename operation: ${oldFile.getName()} to newCSV.csv failed")
    } 
  • 1
    Do you have write permission on the files and on the directory? – khelwood Mar 30 '22 at 15:44
  • Yes I do, I did just notice this in the javadocs though under the renameTO section. "...it might not succeed if a file with the destination abstract pathname already exists." – Steven Rummelhoff Mar 30 '22 at 15:48
  • [How to find out why renameTo() failed?](https://stackoverflow.com/q/1325388/3890632) – khelwood Mar 30 '22 at 16:57
  • Thank you, after renaming the newCSV.csv the code ran successfully. Unsure if it was another user having the file open or if the existence of it caused the issue in the first place. – Steven Rummelhoff Mar 30 '22 at 17:00

1 Answers1

0

It is likely the case that newCSV.csv already exists renameTo is returning false because the rename was unsuccessful.

Jeff Scott Brown
  • 26,804
  • 2
  • 30
  • 47