MCPcopy
hub / github.com/apache/commons-io / moveFile

Method moveFile

src/main/java/org/apache/commons/io/FileUtils.java:2972–3000  ·  view source on GitHub ↗

Moves a file. When the destination file is on another file system, do a "copy and delete". @param srcFile the file to be moved @param destFile the destination file @throws NullPointerException if source or destination is null @throws FileExistsException if the destination file exists

(final File srcFile, final File destFile)

Source from the content-addressed store, hash-verified

2970 * @since 1.4
2971 */
2972 public static void moveFile(final File srcFile, final File destFile) throws IOException {
2973 if (srcFile == null) {
2974 throw new NullPointerException("Source must not be null");
2975 }
2976 if (destFile == null) {
2977 throw new NullPointerException("Destination must not be null");
2978 }
2979 if (!srcFile.exists()) {
2980 throw new FileNotFoundException("Source '" + srcFile + "' does not exist");
2981 }
2982 if (srcFile.isDirectory()) {
2983 throw new IOException("Source '" + srcFile + "' is a directory");
2984 }
2985 if (destFile.exists()) {
2986 throw new FileExistsException("Destination '" + destFile + "' already exists");
2987 }
2988 if (destFile.isDirectory()) {
2989 throw new IOException("Destination '" + destFile + "' is a directory");
2990 }
2991 final boolean rename = srcFile.renameTo(destFile);
2992 if (!rename) {
2993 copyFile(srcFile, destFile);
2994 if (!srcFile.delete()) {
2995 FileUtils.deleteQuietly(destFile);
2996 throw new IOException("Failed to delete original file '" + srcFile +
2997 "' after copy to '" + destFile + "'");
2998 }
2999 }
3000 }
3001
3002 /**
3003 * Moves a file to a directory.

Callers 5

testMoveFile_RenameMethod · 0.95
testMoveFile_ErrorsMethod · 0.95
moveFileToDirectoryMethod · 0.95

Calls 6

copyFileMethod · 0.95
deleteQuietlyMethod · 0.95
existsMethod · 0.80
isDirectoryMethod · 0.80
renameToMethod · 0.80
deleteMethod · 0.45

Tested by 4

testMoveFile_RenameMethod · 0.76
testMoveFile_ErrorsMethod · 0.76