【发布时间】:2012-08-23 00:49:43
【问题描述】:
我正在使用 Win7。我第一次使用java重命名文件(file.cmptr to file.sql)来做一些事情,之后我想用他的旧名(file.cmptr)重命名它
但这失败了,当我重命名文件时,有时无法重命名。它是随机发生的。
-有时第一个文件根本没有变化,我没有得到操作
-有时它更改为 .sql 文件,我得到了操作,但文件保持 .sql
在我的例子中:
-
我第一次更改扩展名:
int dotIndex = filechose.getPath().lastIndexOf('.'); String newpath = filechose.getPath().substring(0, dotIndex);; newpath = newpath + ".sql"; File filefinal = new File(newpath); filechose.renameTo(filefinal); String path=newpath;
在我对新文件进行操作后:
Runtime.getRuntime().exec("cmd /c mysql -u root gestiondestock <"+'"'+path+'"');
最后我想再次重命名文件
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);
解决了。
它适用于等待我只改变这个:
Process p = Runtime.getRuntime().exec("cmd /c mysql -u root gestiondestock <"+'"'+path+'"');
p.waitFor();
【问题讨论】:
-
第二次重命名文件时遇到什么错误?你说“有时失败”,但你没有解释它是如何失败的。
-
@DanPuzey 有时文件根本不重命名没有错误
-