Android Directory Copy copyDirectory(File from, File to)

Here you can find the source of copyDirectory(File from, File to)

Description

Copy a directory and all of its contents.

License

Open Source License

Parameter

Parameter Description
from source file
to target file

Return

success or failure

Declaration

public static boolean copyDirectory(File from, File to) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Main {
    private static final int BUFFER_SIZE = 4096 * 4;

    /**/*from w  ww  .j  ava2  s .c  o m*/
     * Copy a directory and all of its contents.
     * 
     * @param from source file
     * @param to target file
     * @return success or failure
     */
    public static boolean copyDirectory(File from, File to) {
        return copyDirectory(from, to, (byte[]) null);
    }

    public static boolean copyDirectory(String from, String to) {
        return copyDirectory(new File(from), new File(to));
    }

    public static boolean copyDirectory(File from, File to, byte[] buffer) {
        if (from == null)
            return false;
        if (!from.exists())
            return true;
        if (!from.isDirectory())
            return false;
        if (to.exists())
            return false;
        if (!to.mkdirs())
            return false;

        String[] list = from.list();
        // Some JVMs return null for File.list() when the directory is empty.
        if (list != null) {
            if (buffer == null)
                buffer = new byte[BUFFER_SIZE]; // return this buffer to copy files

            for (int i = 0; i < list.length; i++) {
                String fileName = list[i];

                File entry = new File(from, fileName);

                if (entry.isDirectory()) {
                    if (!copyDirectory(entry, new File(to, fileName),
                            buffer)) {
                        return false;
                    }
                } else {
                    if (!copyFile(entry, new File(to, fileName), buffer)) {
                        return false;
                    }
                }
            }
        }

        return true;
    }

    public static boolean copyFile(File from, File to, byte[] buf) {
        if (buf == null)
            buf = new byte[BUFFER_SIZE];

        FileInputStream from_s = null;
        FileOutputStream to_s = null;

        try {
            from_s = new FileInputStream(from);
            to_s = new FileOutputStream(to);

            for (int bytesRead = from_s.read(buf); bytesRead > 0; bytesRead = from_s
                    .read(buf)) {
                to_s.write(buf, 0, bytesRead);
            }

            to_s.getFD().sync();

        } catch (IOException ioe) {
            return false;
        } finally {
            if (from_s != null) {
                try {
                    from_s.close();
                    from_s = null;
                } catch (IOException ioe) {

                }
            }
            if (to_s != null) {
                try {
                    to_s.close();
                    to_s = null;
                } catch (IOException ioe) {
                }
            }
        }

        return true;
    }
}

Related

  1. CopyDir(String sourcedir, String destdir)
  2. copyDir(File source, File destination)
  3. copyDirectory(@NotNull String srcDir, @NotNull String dstDir, @NotNull final List excludes)
  4. copyDirectory(File from, File to, byte[] buffer)
  5. copyDirectory(File source, File destination)
  6. copyDirectory(String from, String to)
  7. copyDirectory(String sourceDirName, String destinationDirName)