Java File Copy nio copy(File inFile, File outFile)

Here you can find the source of copy(File inFile, File outFile)

Description

This method copies one file to another location

License

Apache License

Parameter

Parameter Description
inFile the source filename
outFile the target filename

Return

true on success

Declaration

public static boolean copy(File inFile, File outFile) 

Method Source Code


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

import java.nio.channels.FileChannel;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Main {
    /**/*w ww  . j  av a2s .c o m*/
     * This method copies one file to another location
     *
     * @param inFile the source filename
     * @param outFile the target filename
     * @return true on success
     */
    public static boolean copy(File inFile, File outFile) {
        if (!inFile.exists()) {
            return false;
        }

        FileChannel in = null;
        FileChannel out = null;

        try {
            in = new FileInputStream(inFile).getChannel();
            out = new FileOutputStream(outFile).getChannel();

            long pos = 0;
            long size = in.size();

            while (pos < size) {
                pos += in.transferTo(pos, 10 * 1024 * 1024, out);
            }
        } catch (IOException ioe) {
            return false;
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
                if (out != null) {
                    out.close();
                }
            } catch (IOException ioe) {
                return false;
            }
        }

        return true;

    }
}

Related

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