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

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

问题描述

Java的 File.renameTo()是有问题的,特别是在Windows上,似乎。
作为说,

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.

em>任何文件锁定到要移动的目录的内容,但仍然很常见的是,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.

修改:在这种特殊情况下, c $ c> 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 () 方法。

一个例子:

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天全站免登陆