Android File Copy copyFile(File source, File destination)

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

Description

copy File

Declaration

private static void copyFile(File source, File destination)
            throws IOException 

Method Source Code

//package com.java2s;
import com.google.common.io.Files;
import java.io.File;
import java.io.IOException;

public class Main {
    private static void copyFile(File source, File destination)
            throws IOException {
        Files.copy(source, destination);
    }/*ww w .j  a  v a  2  s.co m*/

    /**
     * Copy one file source to a destination.
     * @param source The file source.
     * @param destination The new path.
     * @throws IOException
     */
    public static void copy(File source, File destination)
            throws IOException {
        copy(source, destination, false);
    }

    /**
     * Copy one file source to a destination.
     * @param source The file source.
     * @param destination The new path.
     * @param force Should this operation overwrite.
     * @throws IOException
     */
    public static void copy(File source, File destination, boolean force)
            throws IOException {
        if (!source.exists()) {
            throw new IllegalArgumentException("Source ("
                    + source.getPath() + ") doesn't exist.");
        }

        if (!force && destination.exists()) {
            throw new IllegalArgumentException("Destination ("
                    + destination.getPath() + ") exists.");
        }

        if (source.isDirectory()) {
            copyDirectory(source, destination);
        } else {
            copyFile(source, destination);
        }
    }

    private static void copyDirectory(File source, File destination)
            throws IOException {
        if (!destination.mkdirs()) {
            throw new IOException(
                    "Failed to create destination directories");
        }

        File[] files = source.listFiles();

        for (File file : files) {
            if (file.isDirectory()) {
                copyDirectory(file, new File(destination, file.getName()));
            } else {
                copyFile(file, new File(destination, file.getName()));
            }
        }
    }
}

Related

  1. copyFile(File from, File to, boolean append)
  2. copyFile(File from, File to, byte[] buf)
  3. copyFile(File in, File out)
  4. copyFile(File source, File dest)
  5. copyFile(File source, File destination)
  6. copyFile(File source, File destination)
  7. copyFile(File source, File target)
  8. copyFile(File sourceFile, File destFile)
  9. copyFile(File src, File dest)