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)
| 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. |