0

I have been writing a program for batch system using quartz and i find a problem,

if(!file.renameTo(new File(PATH_FILE_PROCESSED+file.getName())))System.out.println("Cannot Move File :"+file.getAbsolutePath());
else{
    if(!file.delete())System.out.println("Cannot Delete File :"+file.getAbsolutePath());    
}

i want to move file from one directory to another directory but i can't delete the file in the same process, from my analysis i think the file haven't finished copying then running the file.delete(); my question is, is there a way to wait until the file finished copying then run the file.delete? thanks

Michael Vinci
  • 105
  • 1
  • 8
  • Reading your code, it looks like you go into the `else` block when the `renameTo` call *succeeds*. So, the file does not exist at its original location anymore. So, you cannot delete it from its original location, because it isn't there anymore. – Erwin Bolwidt Aug 02 '16 at 02:41
  • no renameTo in java actually copying the file not moving the file thats why i want to delete when it succeed – Michael Vinci Aug 02 '16 at 07:30
  • If `File.renameTo` copies the file on your computer rather than renaming it, than you have a problem on your platform - it's not supposed to do that. The same problem may also impact the operation of the delete. – Erwin Bolwidt Aug 02 '16 at 08:01

2 Answers2

0

Renaming is almost instant as it doesn't copy the file, it just moves which directory it appears in (unless you are moving between filesystems)

On windows, you can't rename or delete if you have the file open somewhere. Make sure you have close()ed it properly.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
0

i think i already found the problem, the problem is when i insert the the file inside zip some other code already insert the data so it create duplicate in database and it create an error like this. so thanks for answering the question

Michael Vinci
  • 105
  • 1
  • 8