Windows 上的可靠 File.renameTo() 替代方法? [英] Reliable File.renameTo() alternative on Windows?

查看:24
本文介绍了Windows 上的可靠 File.renameTo() 替代方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java 的 File.renameTo() 似乎有问题,尤其是在 Windows 上.作为 API文档说,

Java's File.renameTo() is problematic, especially on Windows, it seems. As the API documentation says,

这个行为的很多方面方法本质上是平台相关:重命名操作可能无法移动文件从一个文件系统到另一个文件系统,它可能不是原子的,它可能如果文件带有目标抽象路径名已经存在.返回值应该总是检查以确保重命名操作成功.

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.

就我而言,作为升级过程的一部分,我需要移动(重命名)一个可能包含千兆字节数据的目录(大量子目录和大小不一的文件).移动总是在同一个分区/驱动器中完成,因此实际上没有必要物理移动磁盘上的所有文件.

In my case, as part of an upgrade procedure, I need to move (rename) a directory that may contain gigabytes of data (lots of subdirectories and files of varying sizes). The move is always done within the same partition/drive, so there's no real need to physically move all the files on disk.

不应该对要移动的目录的内容进行任何文件锁定,但是,renameTo() 仍然经常无法完成其工作并返回 false.(我只是猜测,Windows 上的某些文件锁可能会随意过期.)

There shouldn't be any file locks to the contents of the dir to be moved, but still, quite often, renameTo() fails to do its job and returns false. (I'm just guessing that perhaps some file locks expire somewhat arbitrarily on Windows.)

目前我有一个后备方法,它使用复制 &删除,但这很糟糕,因为它可能需要很多时间,具体取决于文件夹的大小.我也在考虑简单地记录这样一个事实,即用户可以手动移动文件夹以避免等待数小时.但正确的方法显然是自动和快速的.

Currently I have a fallback method that uses copying & deleting, but this sucks because it may take a lot of time, depending on the size of the folder. I'm also considering simply documenting the fact that the user can move the folder manually to avoid waiting for hours, potentially. But the Right Way would obviously be something automatic and quick.

所以我的问题是,您是否知道在 Windows 上使用 Java 进行快速移动/重命名的替代可靠方法,可以使用普通 JDK 或某些外部库.或者,如果您知道一种简单方法来检测和释放给定文件夹及其所有内容(可能是数千个单独文件)的任何文件锁定,那也很好.

So my question is, do you know an alternative, reliable approach to do a quick move/rename with Java on Windows, either with plain JDK or some external library. Or if you know an easy way to detect and release any file locks for a given folder and all of its contents (possibly thousands of individual files), that would be fine too.

编辑:在这种特殊情况下,我们似乎通过考虑更多因素而仅使用 renameTo() 逃脱了;请参阅此答案.

Edit: In this particular case, it seems we got away using just renameTo() by taking a few more things into account; see this answer.

推荐答案

另见 Files.move() JDK 7 中的方法.

See also the Files.move() method in JDK 7.

示例:

String fileName = "MyFile.txt";

try {
    Files.move(new File(fileName).toPath(), new File(fileName).toPath(), java.nio.file.StandardCopyOption.REPLACE_EXISTING);
} catch (IOException ex) {
    Logger.getLogger(SomeClass.class.getName()).log(Level.SEVERE, null, ex);
}

这篇关于Windows 上的可靠 File.renameTo() 替代方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆