Android File Copy copyFile(File source, File target)

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

Description

Copies source file to target file.

License

Open Source License

Parameter

Parameter Description
source - Source file.
target - Target file.

Exception

Parameter Description
InterruptedIOException if thread is interrupted. Attempts to delete partial file if this occurs.
IOException if writing fails.

Declaration

public static void copyFile(File source, File target)
        throws IOException 

Method Source Code

/*/*from www . j  a v a 2s . c o m*/
 * Copyright (c) 2015. Philip DeCamp
 * Released under the BSD 2-Clause License
 * http://opensource.org/licenses/BSD-2-Clause
 */

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class Main{
    private static final String COPY_COMMAND;
    /**
     * Copies source file to target file.
     * 
     * @param source - Source file.
     * @param target - Target file.
     * @throws InterruptedIOException if thread is interrupted.  Attempts to delete partial file if this occurs.
     * @throws IOException if writing fails. 
     */
    public static void copyFile(File source, File target)
            throws IOException {
        Process p = Runtime.getRuntime().exec(
                new String[] { COPY_COMMAND, source.getAbsolutePath(),
                        target.getAbsolutePath() });

        try {
            int ret = p.waitFor();

            if (ret != 0)
                throw new IOException("Failed to copy "
                        + source.getAbsolutePath() + " to "
                        + target.getAbsolutePath());

        } catch (InterruptedException ex) {
            p.destroy();
            target.delete();
            throw new InterruptedIOException(ex.getMessage());
        }
    }
}

Related

  1. copyFile(File in, File out)
  2. copyFile(File source, File dest)
  3. copyFile(File source, File destination)
  4. copyFile(File source, File destination)
  5. copyFile(File source, File destination)
  6. copyFile(File sourceFile, File destFile)
  7. copyFile(File src, File dest)
  8. copyFile(File src, File dst)
  9. copyFile(Path source, Path target)