Java File Copy nio copy(File in, File out, boolean overwrite)

Here you can find the source of copy(File in, File out, boolean overwrite)

Description

Copy a file

License

Apache License

Parameter

Parameter Description
in Input file
out Output file
overwrite If false, only overwrite if in is newer than out

Exception

Parameter Description
IOException an exception

Declaration

public static void copy(File in, File out, boolean overwrite) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Apache 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 va2 s  .c om*/
     * Copy a file
     * 
     * @param in Input file
     * @param out Output file
     * @param overwrite If false, only overwrite if in is newer than
     *            out
     * @throws IOException
     */
    public static void copy(File in, File out, boolean overwrite) throws IOException {
        if (overwrite || !out.exists() || (out.lastModified() < in.lastModified())) {
            FileChannel inChannel = new FileInputStream(in).getChannel();
            FileChannel outChannel = new FileOutputStream(out).getChannel();
            try {
                inChannel.transferTo(0, inChannel.size(), outChannel);
            } finally {
                if (inChannel != null) {
                    inChannel.close();
                }
                if (outChannel != null) {
                    outChannel.close();
                }
            }
        }
    }
}

Related

  1. copy(File from, File to)
  2. copy(File from, File to)
  3. copy(File from, File to)
  4. copy(File from, File to, final boolean recursive, Function excluded)
  5. copy(File in, File out)
  6. copy(File inFile, File outFile)
  7. copy(File input, File output)
  8. copy(File inputFile, OutputStream out)
  9. copy(File source, File dest)