Android Directory Copy copyDir(File source, File destination)

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

Description

copy Dir

Declaration

public static boolean copyDir(File source, File destination) 

Method Source Code

//package com.java2s;

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

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Main {
    public static boolean copyDir(File source, File destination) {
        InputStream in = null;/*from   ww w  . ja  v a2 s . com*/
        OutputStream out = null;

        label222: try {
            if (source.isDirectory()) {
                if (!destination.exists()) {
                    destination.mkdir();
                }
                String[] children = source.list();
                if (children != null) {
                    for (int i = 0; i < children.length; i++) {
                        copyDir(new File(source, children[i]), new File(
                                destination, children[i]));
                    }

                    break label222;
                }
            } else {
                in = new FileInputStream(source);
                out = new FileOutputStream(destination);

                byte[] buf = new byte[1024];
                int len;

                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return true;
    }
}

Related

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