Java FileChannel Copy copyFile(final File in, final File out)

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

Description

Copy the input file to output file

License

Open Source License

Parameter

Parameter Description
in input file
out output file

Exception

Parameter Description
IOException if an I/O exception occurred

Declaration

public static void copyFile(final File in, final File out) 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 {
    /**/*  www . ja v a  2 s . c o  m*/
     * Copy the input file to output file
     *
     * @param in input file
     * @param out output file
     * @throws IOException if an I/O exception occurred
     */
    public static void copyFile(final File in, final File out) throws IOException {
        FileChannel inChannel = new FileInputStream(in).getChannel();
        FileChannel outChannel = new FileOutputStream(out).getChannel();
        try {
            inChannel.transferTo(0, inChannel.size(), outChannel);
        } catch (IOException e) {
            throw e;
        } finally {
            if (inChannel != null) {
                inChannel.close();
            }
            if (outChannel != null) {
                outChannel.close();
            }
        }
    }
}

Related

  1. copyFile(File srcFile, File destFile, boolean preserveFileDate)
  2. copyFile(File srcFile, File destFile, boolean preserveFileDate)
  3. copyFile(File srcFile, File dstFile, boolean overwrite)
  4. copyFile(FileChannel in, FileChannel out)
  5. copyFile(FileInputStream fromFile, FileOutputStream toFile)
  6. copyFile(final File in, final File out)
  7. copyFile(final File input, final File output)
  8. copyFile(final File src, final File dest)
  9. copyFile(final File src, final File dst)