If the rename option fails, then we must try a complete copy since the
move can cross file systems.
This commit is contained in:
Timothy Bish 2015-11-10 11:07:18 -05:00
parent 0c846cf8f6
commit 938aa626c2
1 changed files with 14 additions and 2 deletions

View File

@ -23,12 +23,15 @@ import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
/**
*
* Collection of File and Folder utility methods.
*/
public final class IOHelper {
@ -186,7 +189,16 @@ public final class IOHelper {
public static void moveFile(File src, File targetDirectory) throws IOException {
if (!src.renameTo(new File(targetDirectory, src.getName()))) {
throw new IOException("Failed to move " + src + " to " + targetDirectory);
// If rename fails we must do a true deep copy instead.
Path sourcePath = src.toPath();
Path targetDirPath = targetDirectory.toPath();
try {
Files.move(sourcePath, targetDirPath.resolve(sourcePath.getFileName()), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException ex) {
throw new IOException("Failed to move " + src + " to " + targetDirectory + " - " + ex.getMessage());
}
}
}