Note that there are some explanatory texts on larger screens.

plurals
  1. POReliable File.renameTo() alternative on Windows?
    text
    copied!<p>Java's <code>File.renameTo()</code> is problematic, especially on Windows, it seems. As the <a href="http://java.sun.com/javase/6/docs/api/java/io/File.html#renameTo%28java.io.File%29" rel="noreferrer">API documentation</a> says,</p> <blockquote> <p>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.</p> </blockquote> <p>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.</p> <p>There <em>shouldn't</em> 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.)</p> <p>Currently I have a fallback method that uses copying &amp; deleting, but this sucks because it may take <strong>a lot</strong> 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.</p> <p>So my question is, <strong>do you know an alternative, reliable approach to do a quick move/rename with Java on Windows</strong>, either with plain JDK or some external library. Or if you know an <em>easy</em> way to detect and release any file locks for a given folder and <em>all of its contents</em> (possibly thousands of individual files), that would be fine too.</p> <hr> <p><strong>Edit</strong>: In this particular case, it seems we got away using just <code>renameTo()</code> by taking a few more things into account; see <a href="https://stackoverflow.com/questions/1000183/reliable-file-renameto-alternative-on-windows/1006489#1006489">this answer</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload