public static void rename(@NotNull File source, @NotNull File target) throws IOException { if (source.renameTo(target)) return; if (!source.exists()) return; copy(source, target); delete(source); }
public static boolean moveDirWithContent(@NotNull File fromDir, @NotNull File toDir) { if (!toDir.exists()) return fromDir.renameTo(toDir); File[] files = fromDir.listFiles(); if (files == null) return false; boolean success = true; for (File fromFile : files) { File toFile = new File(toDir, fromFile.getName()); success = success && fromFile.renameTo(toFile); } fromDir.delete(); return success; }
@Nullable private static File renameToTempFileOrDelete(@NotNull File file) { final File tempDir = new File(getTempDirectory()); boolean isSameDrive = true; if (SystemInfo.isWindows) { String tempDirDrive = tempDir.getAbsolutePath().substring(0, 2); String fileDrive = file.getAbsolutePath().substring(0, 2); isSameDrive = tempDirDrive.equalsIgnoreCase(fileDrive); } if (isSameDrive) { // the optimization is reasonable only if destination dir is located on the same drive final String originalFileName = file.getName(); File tempFile = getTempFile(originalFileName, tempDir); if (file.renameTo(tempFile)) { return tempFile; } } delete(file); return null; }