Java FileChannel Copy copyFile(File in, File out)

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

Description

Copy the content of a file to another.

License

Apache License

Parameter

Parameter Description
in the file to copy from
out the file to copy to

Exception

Parameter Description
IOException if a problem occurs when writing to the file

Declaration

public static void copyFile(File in, File out) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.*;

import java.nio.channels.FileChannel;

public class Main {
    /**/*from  ww w  .  j  a v a 2 s .c o m*/
     * Copy the content of a file to another.
     *
     * @param in the file to copy from
     * @param out the file to copy to
     *
     * @throws IOException if a problem occurs when writing to the file
     */
    public static void copyFile(File in, File out) throws IOException {
        copyFile(in, out, true);
    }

    /**
     * Copy the content of one file to another.
     *
     * @param in the file to copy from
     * @param out the file to copy to
     * @param overwrite boolean indicating whether out should be overwritten
     *
     * @throws IOException if a problem occurs when writing to the file
     */
    public static void copyFile(File in, File out, boolean overwrite)
            throws IOException {
        long start = 0;
        if (out.exists() && out.length() > 0) {
            if (overwrite) {
                out.delete();
            } else {
                start = out.length();
            }
        }
        FileChannel inChannel = new FileInputStream(in).getChannel();
        FileChannel outChannel = new FileOutputStream(out).getChannel();
        try {
            inChannel.transferTo(start, inChannel.size(), outChannel);
        } finally {
            if (inChannel != null) {
                inChannel.close();
            }
            if (outChannel != null) {
                outChannel.close();
            }
        }
    }
}

Related

  1. copyFile(File from, File to)
  2. copyFile(File from, File to, long fromoffset, long tooffset, long size)
  3. copyFile(File in, File out)
  4. copyFile(File in, File out)
  5. copyFile(File in, File out)
  6. copyFile(File in, File out)
  7. copyFile(File in, File out)
  8. copyFile(File in, File out)
  9. copyFile(File in, File out)