Android Folder Copy copyFolder(File fromFolder, File toFolder)

Here you can find the source of copyFolder(File fromFolder, File toFolder)

Description

copy Folder

Declaration

private static void copyFolder(File fromFolder, File toFolder)
            throws IOException 

Method Source Code

//package com.java2s;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Main {
    private static void copyFolder(File fromFolder, File toFolder)
            throws IOException {
        if (!fromFolder.exists()) {
            return;
        }//from  w  w  w  .  java2  s  . c  o  m
        if (toFolder.exists()) {
            deleteFileAndFolder(toFolder);
        }
        copyFolder(fromFolder.getAbsolutePath(), toFolder.getAbsolutePath());
    }

    public static void copyFolder(String sourceDir, String targetDir)
            throws IOException {
        (new File(targetDir)).mkdirs();
        File[] file = (new File(sourceDir)).listFiles();
        for (int i = 0; i < file.length; i++) {
            if (file[i].isFile()) {
                File sourceFile = file[i];
                File targetFile = new File(
                        new File(targetDir).getAbsolutePath()
                                + File.separator + file[i].getName());
                copyFile(sourceFile, targetFile);
            }
            if (file[i].isDirectory()) {
                String dir1 = sourceDir + "/" + file[i].getName();
                String dir2 = targetDir + "/" + file[i].getName();
                copyFolder(dir1, dir2);
            }
        }
    }

    private static void deleteFileAndFolder(File fileOrFolder) {
        if (fileOrFolder == null || !fileOrFolder.exists()) {
            return;
        }
        if (fileOrFolder.isDirectory()) {
            File[] children = fileOrFolder.listFiles();
            if (children != null) {
                for (File childFile : children) {
                    deleteFileAndFolder(childFile);
                }
            }
        } else {
            fileOrFolder.delete();
        }
    }

    private static void copyFile(File from, File to) {
        if (!from.exists()) {
            return;
        }
        FileInputStream in = null;
        FileOutputStream out = null;
        try {
            in = new FileInputStream(from);
            out = new FileOutputStream(to);

            byte bt[] = new byte[1024];
            int c;
            while ((c = in.read(bt)) > 0) {
                out.write(bt, 0, c);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}

Related

  1. copyFolder(File src, File dest)
  2. copyFolder(String from, String target, List ingores)
  3. copyFolder(File fromFolder, File toFolder)
  4. copyFolder(String sourceDir, String targetDir)
  5. createFolder(String path)
  6. copyFolder(File fromFolder, File toFolder)
  7. copyFolder(String sourceDir, String targetDir)
  8. copyFolder(String sourceDir, String targetDir)