Java File Move nui moveFile(File src, File dest)

Here you can find the source of moveFile(File src, File dest)

Description

Moves a file from src to dest.

License

Open Source License

Parameter

Parameter Description
src An existing file to move, must not be <code>null</code>.
dest The destination file, must not be <code>null</code> and exist.

Exception

Parameter Description
IOException If moving is failed.

Declaration

public static void moveFile(File src, File dest) throws IOException 

Method Source Code


//package com.java2s;
/*//from ww  w  . ja  v  a 2  s. c  om
 *   casmi
 *   http://casmi.github.com/
 *   Copyright (C) 2011, Xcoo, Inc.
 *
 *  casmi is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import java.nio.channels.FileChannel;

public class Main {
    /**
     * Moves a file from src to dest.
     * This method is equals to "copy -> delete."
     *
     * @param src
     *            An existing file to move, must not be <code>null</code>.
     * @param dest
     *            The destination file, must not be <code>null</code> and exist.
     *
     * @throws IOException
     *             If moving is failed.
     */
    public static void moveFile(File src, File dest) throws IOException {

        copyFile(src, dest);
        if (!delete(src)) {
            throw new IOException("Failed to delete the src file '" + src + "' after copying.");
        }
    }

    /**
     * Copies a file to a new location preserving the file date.
     *
     * @param src
     *            An existing file to copy, must not be <code>null</code>.
     * @param dest
     *            The new file, must not be <code>null</code> and exist.
     *
     * @throws IOException
     *             If copying is failed.
     */
    public static void copyFile(File src, File dest) throws IOException {

        if (src == null)
            throw new NullPointerException("Source must not be null");
        if (dest == null)
            throw new NullPointerException("Destination must not be null");
        if (!src.exists())
            throw new FileNotFoundException("Source '" + src + "' does not exist");
        if (!src.isFile())
            throw new IOException("Source '" + src + "' is not a file");
        if (dest.exists())
            throw new IOException("Destination '" + dest + "' is already exists");

        FileChannel in = null, out = null;
        try {
            in = new FileInputStream(src).getChannel();
            out = new FileOutputStream(dest).getChannel();

            in.transferTo(0, in.size(), out);
        } finally {
            try {
                in.close();
                out.close();
            } catch (IOException e) {
                // Ignore.
            }
        }

        if (src.length() != dest.length()) {
            throw new IOException("Failed to copy full contents from '" + src + "' to '" + dest + "'");
        }

        dest.setLastModified(src.lastModified());
    }

    /**
     * Deletes a file.
     *
     * @param file A file to delete.
     *
     * @return <code>true</code> if the file was deleted, otherwise
     *         <code>false</code>.
     */
    public static boolean delete(File file) {

        if (file == null) {
            return false;
        } else if (!file.isFile()) {
            return false;
        }

        return file.delete();
    }
}

Related

  1. move(File source, File destination)
  2. move(final File from, final File to, final boolean replace)
  3. move(String sourceFile, String targetFile)
  4. moveFile(File from, File to)
  5. moveFile(File source, File destination)
  6. moveFile(final File srcFile, final File destFile)
  7. moveFile(String source, String target)
  8. moveFile(String sourceFile, String destFile)
  9. moveFiles(File src, String newSrc, File dest)