Java FileChannel Copy copyFile(File source, File dest)

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

Description

Copies one file to another one.

License

Open Source License

Parameter

Parameter Description
source a parameter
dest a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static void copyFile(File source, File dest) throws IOException 

Method Source Code


//package com.java2s;

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

import java.nio.channels.FileChannel;

public class Main {
    private static final long MAX_COPY_COUNT = (64L * 1024L * 1024L) - (32L * 1024L);

    /**/*from   ww  w.j a  v a2 s  .co  m*/
     * Copies one file to another one.
     * 
     * @param source
     * @param dest
     * 
     * @throws IOException
     */
    public static void copyFile(File source, File dest) throws IOException {
        if (dest.exists())
            throw new IOException("Destination file already exists.");

        FileChannel in = null;
        FileChannel out = null;

        try {
            in = new FileInputStream(source).getChannel();
            out = new FileOutputStream(dest).getChannel();

            long size = in.size();
            long position = 0L;

            while (position < size)
                position += in.transferTo(position, MAX_COPY_COUNT, out);

            dest.setLastModified(source.lastModified());
        } finally {
            if (in != null)
                in.close();

            if (out != null)
                out.close();
        }
    }
}

Related

  1. copyFile(File in, File out)
  2. copyfile(File infile, File outfile)
  3. copyFile(File inFile, File outFile)
  4. copyFile(File inputFile, File outputFile)
  5. copyFile(File source, File dest)
  6. copyFile(File source, File dest)
  7. copyFile(File source, File destination)
  8. copyFile(File source, File destination)
  9. copyFile(File source, File destination)