Java File Copy nio copy(File sourceFile, File destinationFile)

Here you can find the source of copy(File sourceFile, File destinationFile)

Description

Copies one file to another.

License

Open Source License

Parameter

Parameter Description
sourceFile the source file to copy
destinationFile the destination file to copy to

Exception

Parameter Description
IOException If an input or output exception occurred

Declaration

public static void copy(File sourceFile, File destinationFile) throws IOException 

Method Source Code

//package com.java2s;
/*//from   w w  w .  j av a 2 s  .  c  o m
 *  This file is part the Cytobank ACS Library.
 *  Copyright (C) 2010 Cytobank, Inc.  All rights reserved.
 *
 *  The Cytobank ACS Library program 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.FileOutputStream;
import java.io.IOException;

import java.nio.channels.FileChannel;

public class Main {
    /**
     * Copies one file to another.
     * 
     * @param sourceFile the source file to copy
     * @param destinationFile the destination file to copy to
     * @throws IOException If an input or output exception occurred
     */
    public static void copy(File sourceFile, File destinationFile) throws IOException {
        FileChannel sourceChannel = null;
        FileChannel destinationChannel = null;
        try {
            sourceChannel = new FileInputStream(sourceFile).getChannel();
            destinationChannel = new FileOutputStream(destinationFile).getChannel();
            destinationChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
        } finally {
            if (sourceChannel != null) {
                try {
                    sourceChannel.close();
                } catch (Throwable ignore) {
                }
            }
            if (destinationChannel != null) {
                try {
                    destinationChannel.close();
                } catch (Throwable ignore) {
                }
            }
        }
    }
}

Related

  1. copy(File source, File target)
  2. copy(File source, File target)
  3. copy(File source, File target, FilenameFilter filter)
  4. copy(File sourceFile, File destFile)
  5. copy(File sourceFile, File destFile)
  6. copy(File sourceFile, File targetFile)
  7. copy(File src, File dest)
  8. copy(File src, File dest)
  9. copy(File src, File dest)