Java FileChannel Copy copyFile(File sourceFile, File destFile)

Here you can find the source of copyFile(File sourceFile, File destFile)

Description

Copies a single file from one location to another

License

Open Source License

Parameter

Parameter Description
from The origin file
to The destination file

Return

Returns true if the file was able to be copied successfully

Declaration

public static boolean copyFile(File sourceFile, File destFile) 

Method Source Code

//package com.java2s;
/**/*  w  w  w .  ja va 2 s .  c  o  m*/
 * This class is the core Single Player Commands class. It handles all input and controls all
 * output
 * 
 * @author simo_415 Copyright (C) 2010-2012 simo_415 - (http://bit.ly/spcmod)
 * 
 *         This file is part of Single Player Commands.
 * 
 *         Single Player Commands 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.
 * 
 *         Single Player Commands 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
 *         Single Player Commands. If not, see <http://www.gnu.org/licenses/>.
 */

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

import java.io.IOException;

import java.nio.channels.FileChannel;

public class Main {
    /**
     * Copies a single file from one location to another
     * 
     * @param from The origin file
     * @param to The destination file
     * @return Returns true if the file was able to be copied successfully
     */
    public static boolean copyFile(File sourceFile, File destFile) {
        if (!destFile.exists()) {
            try {
                destFile.createNewFile();
            } catch (IOException e) {
                return false;
            }
        }

        FileChannel source = null;
        FileChannel destination = null;
        try {
            source = new FileInputStream(sourceFile).getChannel();
            destination = new FileOutputStream(destFile).getChannel();
            destination.transferFrom(source, 0, source.size());
        } catch (Exception e) {
            return false;
        } finally {
            try {
                if (source != null) {
                    source.close();
                }
                if (destination != null) {
                    destination.close();
                }
            } catch (Exception e) {
            }
        }
        return true;
    }
}

Related

  1. copyFile(File sourceFile, File destFile)
  2. copyFile(File sourceFile, File destFile)
  3. copyFile(File sourceFile, File destFile)
  4. copyFile(File sourceFile, File destFile)
  5. copyFile(File sourceFile, File destFile)
  6. copyFile(File sourceFile, File destinationFile)
  7. copyFile(File sourceFile, File destinationFile)
  8. copyFile(File sourceFile, File destinationFile)
  9. copyFile(File sourceFile, File targetFile)