Here you can find the source of moveFile(final File srcFile, final File destFile)
Parameter | Description |
---|---|
srcFile | File to copy |
destFile | Destination file |
Parameter | Description |
---|---|
IOException | if an error occurs while copying file |
public static boolean moveFile(final File srcFile, final File destFile) throws IOException
//package com.java2s; /*/* www . j a v a2 s .c om*/ * Eoulsan development code * * This code may be freely distributed and modified under the * terms of the GNU Lesser General Public License version 2.1 or * later and CeCILL-C. This should be distributed with the code. * If you do not have a copy, see: * * http://www.gnu.org/licenses/lgpl-2.1.txt * http://www.cecill.info/licences/Licence_CeCILL-C_V1-en.txt * * Copyright for this code is held jointly by the Genomic platform * of the Institut de Biologie de l'?cole normale sup?rieure and * the individual authors. These should be listed in @author doc * comments. * * For more information on the Eoulsan project and its aims, * or to join the Eoulsan Google group, visit the home page * at: * * http://outils.genomique.biologie.ens.fr/eoulsan * */ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.channels.FileChannel; public class Main { /** * Copy a file. * @param srcFile File to copy * @param destFile Destination file * @throws IOException if an error occurs while copying file */ public static boolean moveFile(final File srcFile, final File destFile) throws IOException { return moveFile(srcFile, destFile, true); } /** * Copy a file. * @param srcFile File to copy * @param destFile Destination file * @param overwrite overwrite existing file * @throws IOException if an error occurs while copying file */ public static boolean moveFile(final File srcFile, final File destFile, final boolean overwrite) throws IOException { return copyFile(srcFile, destFile, overwrite) && srcFile.delete(); } /** * Copy a file. * @param srcFile File to copy * @param destFile Destination file * @throws IOException if an error occurs while copying file */ public static boolean copyFile(final File srcFile, final File destFile) throws IOException { return copyFile(srcFile, destFile, false); } /** * Copy a file. * @param srcFile File to copy * @param destFile Destination file * @param overwrite overwrite existing file * @throws IOException if an error occurs while copying file */ public static boolean copyFile(final File srcFile, final File destFile, final boolean overwrite) throws IOException { if (srcFile == null) { throw new NullPointerException("Input file is null"); } if (destFile == null) { throw new NullPointerException("output file is null"); } if (!srcFile.exists()) { throw new IOException("Source file doesn't exists: " + srcFile); } if (srcFile.isDirectory()) { throw new IOException("Can't copy/move a directory: " + srcFile); } final File myDestFile; if (destFile.isDirectory()) { myDestFile = new File(destFile, srcFile.getName()); } else { myDestFile = destFile; } if (destFile.exists()) { if (!overwrite) { return false; } if (!myDestFile.delete()) { throw new IOException("Can not remove existing file: " + myDestFile.getAbsolutePath()); } } final FileChannel inChannel = new FileInputStream(srcFile).getChannel(); final FileChannel outChannel = new FileOutputStream(myDestFile).getChannel(); try { inChannel.transferTo(0, inChannel.size(), outChannel); } finally { if (inChannel != null) { inChannel.close(); } if (outChannel != null) { outChannel.close(); } } return true; } }