Android Folder Copy copyFolder(String sourceDir, String targetDir)

Here you can find the source of copyFolder(String sourceDir, String targetDir)

Description

copy Folder

Declaration

public static void copyFolder(String sourceDir, String targetDir)
            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 .java2s.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 fromFolder, File toFolder)
  2. copyFolder(String sourceDir, String targetDir)
  3. createFolder(String path)
  4. copyFolder(File fromFolder, File toFolder)
  5. copyFolder(File fromFolder, File toFolder)
  6. copyFolder(String sourceDir, String targetDir)