Java FileChannel Copy copyFile(FileChannel in, FileChannel out)

Here you can find the source of copyFile(FileChannel in, FileChannel out)

Description

Optimized copy from one file to another

License

Open Source License

Parameter

Parameter Description
in the input file channel
out the output file channel
size the size of the file

Exception

Parameter Description
IOException if there is an error reading or writing

Declaration

public static void copyFile(FileChannel in, FileChannel out) throws IOException 

Method Source Code

//package com.java2s;
/**/*from ww w .j  av a2 s.c  o  m*/
 * Project Wonderland
 *
 * Copyright (c) 2004-2009, Sun Microsystems, Inc., All Rights Reserved
 *
 * Redistributions in source code form must reproduce the above
 * copyright and this condition.
 *
 * The contents of this file are subject to the GNU General Public
 * License, Version 2 (the "License"); you may not use this file
 * except in compliance with the License. A copy of the License is
 * available at http://www.opensource.org/licenses/gpl-license.php.
 *
 * Sun designates this particular file as subject to the "Classpath" 
 * exception as provided by Sun in the License file that accompanied 
 * this code.
 */

import java.io.IOException;

import java.nio.channels.FileChannel;

public class Main {
    /** 
     * Optimized copy from one file to another
     * @param in the input file channel
     * @param out the output file channel
     * @param size the size of the file
     * @throws IOException if there is an error reading or writing
     */
    public static void copyFile(FileChannel in, FileChannel out) throws IOException {
        long position = 0;
        long read;

        while ((read = in.transferTo(position, 16 * 1024, out)) > 0) {
            position += read;
        }
    }
}

Related

  1. copyFile(File src, File target)
  2. copyFile(File srcFile, File destFile, boolean preserveFileDate)
  3. copyFile(File srcFile, File destFile, boolean preserveFileDate)
  4. copyFile(File srcFile, File destFile, boolean preserveFileDate)
  5. copyFile(File srcFile, File dstFile, boolean overwrite)
  6. copyFile(FileInputStream fromFile, FileOutputStream toFile)
  7. copyFile(final File in, final File out)
  8. copyFile(final File in, final File out)
  9. copyFile(final File input, final File output)