7/1/2023 0 Comments Java file renamer![]() ![]() Perhaps even do a simple retry mechanism that tries renameTo(), and then waits for a period (which maybe increases gradually), until the operation succeeds or some timeout is reached. (In one point in our installer/upgrader we added a "sleep" action and an indeterminate progress bar for some 10 seconds, because there might be a service hanging on to some files). If you suspect there may be some lingering file locks, waiting a little before the move/rename might help. (Obviously I shouldn't have assumed this kind of thing works the same across platforms this is exactly what the Javadoc warns about.) This surprised me, as I had tried on Linux, where renameTo() succeeded if target existed, as long as it was empty. On Windows, renameTo() seems to fail if the target directory exists, even if it's empty. ![]() 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.Įdit: In this particular case, it seems we got away using just renameTo() by taking a few more things into account see this answer.įor what it's worth, some further notions: 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. But the Right Way would obviously be something automatic and quick. I'm also considering simply documenting the fact that the user can move the folder manually to avoid waiting for hours, potentially. (I'm just guessing that perhaps some file locks expire somewhat arbitrarily on Windows.)Ĭurrently 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. 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. The move is always done within the same partition/drive, so there's no real need to physically move all the files on disk. 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). Java's File.renameTo() is problematic, especially on Windows, it seems. ![]()
0 Comments
Leave a Reply. |