Android File Copy 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

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;
import java.nio.channels.FileChannel;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Main {
    /**//from   w ww.  j a  va  2 s .  c om
     * 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) {
            ioe.printStackTrace();
            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 pSourceFile, File pTargetFile)
  2. copy(File source, File target, boolean append)
  3. copy(File sourceFile, File destinationFile)
  4. copy(File sourceFile, File targetFile)