Java FileChannel Copy copyFile(final File input, final File output)

Here you can find the source of copyFile(final File input, final File output)

Description

This method can be used to copy a file from one place to another.

License

Open Source License

Parameter

Parameter Description
input The file to be read
output The file to be written to

Exception

Parameter Description
IOException if anything fails.

Declaration

public static void copyFile(final File input, final File output) 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 {
    /**/* ww  w .ja v  a2 s  .c  om*/
     * This method can be used to copy a file from one place to another.
     * 
     * @param input
     *            The file to be read
     * @param output
     *            The file to be written to
     * @throws IOException
     *             if anything fails.
     */
    public static void copyFile(final File input, final File output) throws IOException {
        FileInputStream in = null;
        FileOutputStream out = null;

        try {
            in = new FileInputStream(input);
            out = new FileOutputStream(output);
            FileChannel inChannel = in.getChannel();
            FileChannel outChannel = out.getChannel();
            long size = inChannel.size();
            inChannel.transferTo(0L, size, outChannel);
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException e) {
                /* ignore */
            }
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                /* ignore */
            }
        }
    }
}

Related

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