Java FileChannel Copy copyFile(File inputFile, File outputFile)

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

Description

Copies a file to the specified location.

License

Open Source License

Parameter

Parameter Description
inputFile The file to be copied.
outputFile The directory the file should be copied to.

Declaration

public static void copyFile(File inputFile, File outputFile) throws IOException 

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 {
    /**/*from w  ww .j  a v a  2s. c  o m*/
     * Copies a file to the specified location.
     * @param inputFile      The file to be copied.
     * @param outputFile The directory the file should be copied to.
     */
    public static void copyFile(File inputFile, File outputFile) throws IOException {
        if (inputFile.isDirectory()) {
            throw new IOException("Cannot copy file: " + inputFile + " as this is a directory");
        }
        if (outputFile.isDirectory()) {
            outputFile = new File(outputFile, inputFile.getName());
        }

        outputFile.getParentFile().mkdirs();
        outputFile.createNewFile();

        FileInputStream in = new FileInputStream(inputFile);
        FileOutputStream out = new FileOutputStream(outputFile);

        FileChannel inc = in.getChannel();
        FileChannel outc = out.getChannel();

        inc.transferTo(0, inc.size(), outc);

        inc.close();
        outc.close();

        in.close();
        out.close();
    }
}

Related

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