Java FileChannel Copy copyFile(File source, File destination)

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

Description

copy File

License

Open Source License

Declaration

private static void copyFile(File source, File destination) throws FileNotFoundException, IOException 

Method Source Code

//package com.java2s;

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

import java.nio.channels.FileChannel;

public class Main {
    private static void copyFile(File source, File destination) throws FileNotFoundException, IOException {
        FileChannel inputChannel = null;
        FileChannel outputChannel = null;
        try {/*from   w  ww. j a  v  a 2 s .  com*/
            inputChannel = new FileInputStream(source).getChannel();
            outputChannel = new FileOutputStream(destination).getChannel();
            final long bytesToCopy = inputChannel.size();
            final long bytesCopied = inputChannel.transferTo(0, bytesToCopy, outputChannel);
            if (bytesToCopy != bytesCopied) {
                throw new IOException("FileChannel.transferTo() failed");
            }
        } finally {
            if (inputChannel != null) {
                inputChannel.close();
            }
            if (outputChannel != null) {
                outputChannel.close();
            }
        }
    }
}

Related

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