File renameTo & File.delete

707 Views Asked by At

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

2

There are 2 best solutions below

7
Peter Lawrey On

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.

0
Michael Vinci On

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