如
javadoc中所述:
Renames the file denoted by this abstract pathname. Many aspects of
the behavior of this method are inherently platform-dependent: The
rename operation might not be able to move a file from one filesystem
to another, it might not be atomic, and it might not succeed if a file
with the destination abstract pathname already exists. The return
value should always be checked to make sure that the rename operation
was successful.
这是一个例子:
// The File (or directory) with the old name
File oldFile = new File("old.txt");
// The File (or directory) with the new name
File newFile = new File("new.txt");
// Rename file (or directory)
boolean success = oldFile.renameTo(newFile);
if (!success) {
// File was not successfully renamed
}
我的建议是检查成功布尔值并使用API中定义的标准方法.