为什么 File.renameTo 不改变 File 指向的位置?
File oldFile = new File("old");
if (oldFile.renameTo(new File("new"))){
System.out.println(oldFile.getName());//this prints "old"
}
我查看了 openJDK 源代码,其中 renameTo(File dest) 函数如下所示:
public class File implements Serializable, Comparable<File> {
static private FileSystem fs = FileSystem.getFileSystem();
private String path;
...
public boolean renameTo(File dest) {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkWrite(path);
security.checkWrite(dest.path);
}
return fs.rename(this, dest);
}
...
}
所以路径变量永远不会改变。为什么会这样呢?使用重命名的文件变量的正确方法是什么?目前我是这样做的:
File oldFile = new File("/home/blin/misk/old");
File newFile = new File("/home/blin/misk/new");
if (oldFile.renameTo(newFile)){
oldFile=newFile;
System.out.println(oldFile.getName());//this prints "new"
}
File oldFile = new File("old");
if (oldFile.renameTo(new File("new"))){
System.out.println(oldFile.getName());//this prints "old"
}
I've looked at openJDK source, and there renameTo(File dest) function looks like this:
public class File implements Serializable, Comparable<File> {
static private FileSystem fs = FileSystem.getFileSystem();
private String path;
...
public boolean renameTo(File dest) {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkWrite(path);
security.checkWrite(dest.path);
}
return fs.rename(this, dest);
}
...
}
So the path variable never gets changed. Why is that so? What would be the right way to use renamed File variable? Currently i do it like this:
File oldFile = new File("/home/blin/misk/old");
File newFile = new File("/home/blin/misk/new");
if (oldFile.renameTo(newFile)){
oldFile=newFile;
System.out.println(oldFile.getName());//this prints "new"
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
最简单的解释是, 引用 Javadoc :
正如其他人所说,这里没有对错。然而,一旦库的设计者做出上述选择,当前的
renameTo
行为就成为唯一可能的行为。至于你的第二个代码片段,我看不出它有任何缺陷。
The simplest possible explanation is that, to quote the Javadoc:
As others have said, there is no right or wrong here. However, as soon as the library's designers made the above choice, the current behaviour of
renameTo
became the only possible one.As to your second code snippet, I can see no flaws in it.
File 对象只是一个名称,它甚至不必存在。
renameTo API 调用实际上重命名文件系统上的文件,但不会更改文件对象,因为这是 API 的设计目的。这里没有对错之分。 Sun 的 API 设计者认为这种方式更有意义。
A File object is just a name, it does not even have to exist.
The renameTo API call actually renames the file on the file system, but does not alter the File Object because this is what the API is designed to do. there is no right or wrong here. the API designers at Sun thought that it makes more sense this way.
快速浏览一下文件,它看起来是不可变的。它有一些设置器,但它们对文件系统上的实际文件进行操作,而不是对 File 实例进行操作。
因此重命名不修改当前实例保持相同的样式。
From quick glance into File, it looks like its immutable. It has some setters, but they operate on actual file on filesystem, not on the File instance.
So rename not modifiing current instance keeps the same style.