Adel Bachene
Adel Bachene

Reputation: 994

when we use renameTo() 2 times failed, why?

I am using Win7. I use java to rename a file the 1st time (file.cmptr to file.sql) to do some thing, after that I want to rename it in his old name (file.cmptr)

but this failed, when I rename the files, some times failed to be renamed. It happen randomly.

-some times the 1st file don't change at all and i don't get the operation

-and some times it change to .sql file and I get the operation but the file stay .sql

in my example:

after i do the operation with the new file:

Runtime.getRuntime().exec("cmd /c mysql -u root gestiondestock <"+'"'+path+'"');

finally i'm trying to rename the file again

File file1 = new File(path);
int dotIndex1 = path.lastIndexOf('.');
String newpath1 = path.substring(0, dotIndex1);
newpath1 = newpath1 + ".computeramg";
File file2 = new File(newpath1); 
file1.renameTo(file2);

Solved.
it work with wait for i change only this:

  Process p = Runtime.getRuntime().exec("cmd /c mysql -u root gestiondestock <"+'"'+path+'"');

                 p.waitFor();

Upvotes: 0

Views: 244

Answers (1)

Peter Lawrey
Peter Lawrey

Reputation: 533820

I suspect you are trying to rename the file while it is being using.

You can use waitFor on the mysql process, or you can add the rename using REN to the script you are executing. i.e. so it will rename the file when it has finished.

Upvotes: 3

Related Questions