Java File Copy nio copy(File source, File destination)

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

Description

Copies a file from source to destination.

License

Open Source License

Declaration

public static void copy(File source, File destination)
        throws IOException 

Method Source Code

//package com.java2s;
/**//from   w  ww  .j  a  v a 2s. c o  m
 * AC - A source-code copy detector
 *
 *     For more information please visit:  http://github.com/manuel-freire/ac
 *
 * ****************************************************************************
 *
 * This file is part of AC, version 2.0
 *
 * AC 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.
 *
 * AC 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 AC.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.*;

public class Main {
    /**
     * Copies a file from source to destination. Uses then 'nio' package
     * for great justice (well, actually only compactness and speed).
     */
    public static void copy(File source, File destination)
            throws IOException {
        FileInputStream in = new FileInputStream(source);
        FileOutputStream out = new FileOutputStream(destination);
        byte[] buffer = new byte[1024 * 16];
        int len;
        try {
            while ((len = in.read(buffer)) != -1)
                out.write(buffer, 0, len);
        } finally {
            in.close();
            out.close();
        }
        //        Fails on my SDK, unknown reason...        
        //        java.nio.channels.FileChannel in = null, out = null;
        //        try {          
        //            in = new FileInputStream(source).getChannel();
        //            log.debug("Source size is "+in.size());
        //            out = new FileOutputStream(destination).getChannel();
        //            log.debug("Dest size is "+out.size());
        //            in.transferTo(0, in.size(), out);
        //        } finally {
        //            if (in != null) in.close();
        //            if (out != null) out.close();
        //        }
    }
}

Related

  1. copy(File source, File dest)
  2. copy(File source, File dest)
  3. copy(File source, File dest)
  4. copy(File source, File dest, boolean preserveTime)
  5. copy(File source, File destination)
  6. copy(File source, File destination)
  7. copy(File source, File destination)
  8. copy(File source, File destination)
  9. copy(File source, File destination)