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

public static void copyFile(File source, File destination) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

import java.nio.channels.FileChannel;

public class Main {
    public static void copyFile(File source, File destination) {
        try {//from   w w  w  .  j  a  v  a 2  s. co  m
            // From http://java.sun.com/developer/JDCTechTips/2002/tt0507.html.
            FileInputStream fileInputStream = new FileInputStream(source);
            FileOutputStream fileOutputStream = new FileOutputStream(destination);
            FileChannel fileInputChannel = fileInputStream.getChannel();
            FileChannel fileOutputChannel = fileOutputStream.getChannel();

            fileInputChannel.transferTo(0, fileInputChannel.size(), fileOutputChannel);

            fileInputChannel.close();
            fileOutputChannel.close();
            fileInputStream.close();
            fileOutputStream.close();
        } catch (IOException ex) {
            throw new RuntimeException("Couldn't copy " + source + " to " + destination + ": " + ex.getMessage());
        }
    }
}

Related

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