Java FileChannel Copy copyFile(final String src, final String dst)

Here you can find the source of copyFile(final String src, final String dst)

Description

Copies source file content into destination file.

License

Open Source License

Parameter

Parameter Description
src source file path
dst destination file path

Return

true if copy operation succeed, false otherwise

Declaration

public static boolean copyFile(final String src, final String dst) 

Method Source Code

//package com.java2s;
/*//ww w . j av  a2  s. co m
 * This file is part of WebLookAndFeel library.
 *
 * WebLookAndFeel library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * WebLookAndFeel library 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with WebLookAndFeel library.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.*;

import java.nio.channels.FileChannel;

import java.util.*;

public class Main {
    /**
     * Cache for "isFile" method result.
     */
    private static final Map<String, Boolean> isFileCache = new HashMap<String, Boolean>();

    /**
     * Copies source file content into destination file.
     * If destination directory doesn't exist it will be created in the process.
     * If destination file doesn't exist it will be also created in the process.
     *
     * @param src source file path
     * @param dst destination file path
     * @return true if copy operation succeed, false otherwise
     */
    public static boolean copyFile(final String src, final String dst) {
        try {
            // Creating destination directory if needed
            final File dstDir = new File(new File(dst).getParent());
            if (ensureDirectoryExists(dstDir)) {
                final FileChannel srcFC = new FileInputStream(src).getChannel();
                final FileChannel dstFC = new FileOutputStream(dst).getChannel();
                return copyFile(srcFC, dstFC);
            } else {
                return false;
            }

        } catch (final FileNotFoundException e) {
            return false;
        }
    }

    /**
     * Copies source file content into destination file.
     * If destination directory doesn't exist it will be created in the process.
     * If destination file doesn't exist it will be also created in the process.
     *
     * @param srcFile source file
     * @param dstFile destination file
     * @return true if copy operation succeed, false otherwise
     */
    public static boolean copyFile(final File srcFile, final File dstFile) {
        if (srcFile.exists() && srcFile.isFile()) {
            try {
                // Creating destination directory if needed
                final File dstDir = new File(dstFile.getParent());
                if (ensureDirectoryExists(dstDir)) {
                    final FileChannel srcFC = new FileInputStream(srcFile).getChannel();
                    final FileChannel dstFC = new FileOutputStream(dstFile).getChannel();
                    return copyFile(srcFC, dstFC);
                } else {
                    return false;
                }
            } catch (final FileNotFoundException e) {
                return false;
            }
        } else {
            return false;
        }
    }

    /**
     * Copies file data from source file channel into destination file channel.
     *
     * @param srcFC source file channel
     * @param dstFC destination file channel
     * @return true if copy operation succeed, false otherwise
     */
    public static boolean copyFile(final FileChannel srcFC, final FileChannel dstFC) {
        try {
            dstFC.transferFrom(srcFC, 0, srcFC.size());
            srcFC.close();
            dstFC.close();
            return true;
        } catch (final IOException e) {
            return false;
        }
    }

    /**
     * Returns true if directory exists or was successfully created during this check, false otherwise.
     *
     * @param dir path to directory to check
     * @return true if directory exists or was successfully created during this check, false otherwise
     */
    public static boolean ensureDirectoryExists(final String dir) {
        return ensureDirectoryExists(new File(dir));
    }

    /**
     * Returns true if directory exists or was successfully created during this check, false otherwise.
     *
     * @param dir directory to check
     * @return true if directory exists or was successfully created during this check, false otherwise
     */
    public static boolean ensureDirectoryExists(final File dir) {
        return dir.exists() || dir.mkdirs();
    }

    /**
     * Returns whether the specified file is actually a file (and not a directory, disk or some system folder) or not.
     *
     * @param file file to process
     * @return true if the specified file is actually a file, false otherwise
     */
    public static boolean isFile(final File file) {
        if (file == null) {
            return false;
        } else if (isFileCache.containsKey(file.getAbsolutePath())) {
            return isFileCache.get(file.getAbsolutePath());
        } else {
            final boolean isFile = file.isFile();
            isFileCache.put(file.getAbsolutePath(), isFile);
            return isFile;
        }
    }
}

Related

  1. copyFile(final File src, final File dest)
  2. copyFile(final File src, final File dst)
  3. copyFile(final File srcFile, final File destFile)
  4. copyFile(final File srcFile, final File destFile)
  5. copyFile(final String src, final String dest)
  6. copyFile(InputStream is, OutputStream os)
  7. copyFile(java.io.File fromFile, java.io.File toFile)
  8. copyFile(java.io.File in, java.io.File out)
  9. copyFile(Path source, Path target)