Java FileChannel Copy copyFile(File in, File out)

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

Description

Copy a file from one location to another.

License

Open Source License

Parameter

Parameter Description
in a parameter
out a parameter

Exception

Parameter Description
IOException an exception

Declaration

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

Method Source Code


//package com.java2s;
/*/*from  w ww .ja  v a  2s. c  o  m*/
 * Phon - An open source tool for research in phonology.
 * Copyright (C) 2005 - 2015, Gregory Hedlund <ghedlund@mun.ca> and Yvan Rose <yrose@mun.ca>
 * Dept of Linguistics, Memorial University <https://phon.ca>
 * 
 * This program 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.
 * 
 * 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU 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 {
    /**
     * Copy a file from one location to another.  Can
     * handle large files.
     * 
     * @param in
     * @param out
     * @throws IOException
     */
    public static void copyFile(File in, File out) throws IOException {
        FileChannel inChannel = new FileInputStream(in).getChannel();
        FileChannel outChannel = new FileOutputStream(out).getChannel();

        long dataTransferred = 0;
        long blockSize = 1024 * 1024; // 1MB
        try {
            while (dataTransferred < inChannel.size()) {

                long dataLeft = inChannel.size() - dataTransferred;
                long toSend = (dataLeft < blockSize ? dataLeft : blockSize);

                dataTransferred += inChannel.transferTo(dataTransferred, toSend, outChannel);
            }

            if (dataTransferred != inChannel.size()) {
                throw new IOException("Data transfer not complete.  Souce size:" + inChannel.size()
                        + " bytes transferred: " + dataTransferred);
            }
        } catch (IOException e) {
            throw e;
        } finally {
            if (inChannel != null)
                inChannel.close();
            if (outChannel != null)
                outChannel.close();
        }
    }
}

Related

  1. copyFile(File from, File to)
  2. copyFile(File from, File to)
  3. copyFile(File from, File to, long fromoffset, long tooffset, long size)
  4. copyFile(File in, File out)
  5. copyFile(File in, File out)
  6. copyFile(File in, File out)
  7. copyFile(File in, File out)
  8. copyFile(File in, File out)
  9. copyFile(File in, File out)