为什么 File.renameTo 不改变 File 指向的位置?

发布于 2024-12-03 04:50:26 字数 987 浏览 2 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情绪少女 2024-12-10 04:50:26

最简单的解释是, 引用 Javadoc

File 类的实例是不可变的;也就是说,一旦创建,File 对象表示的抽象路径名将永远不会改变。

正如其他人所说,这里没有对错。然而,一旦库的设计者做出上述选择,当前的 renameTo 行为就成为唯一可能的行为。

至于你的第二个代码片段,我看不出它有任何缺陷。

The simplest possible explanation is that, to quote the Javadoc:

Instances of the File class are immutable; that is, once created, the abstract pathname represented by a File object will never change.

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.

梦忆晨望 2024-12-10 04:50:26

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.

唔猫 2024-12-10 04:50:26

快速浏览一下文件,它看起来是不可变的。它有一些设置器,但它们对文件系统上的实际文件进行操作,而不是对 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文