Android Directory Copy copyDirectory(File sourceDir, File targetDir)

Here you can find the source of copyDirectory(File sourceDir, File targetDir)

Description

copy Directory

Declaration

public static void copyDirectory(File sourceDir, File targetDir)
            throws IOException 

Method Source Code

//package com.java2s;

import java.io.*;

public class Main {
    private final static int BUFFER = 8192;

    public static void copyDirectory(File sourceDir, File targetDir)
            throws IOException {
        //??????/* w ww  .j a  va  2  s  .  com*/
        targetDir.mkdirs();
        if (null != sourceDir) {
            //????????????????
            File[] file = sourceDir.listFiles();
            for (int i = 0; i < file.length; i++) {
                if (file[i].isFile()) {
                    File sourceFile = file[i];
                    File targetFile = new File(targetDir.getAbsolutePath()
                            + File.separator + file[i].getName());
                    copyFile(sourceFile, targetFile);
                } else if (file[i].isDirectory()) {
                    File dir1 = new File(sourceDir, file[i].getName());
                    File dir2 = new File(targetDir, file[i].getName());
                    copyDirectory(dir1, dir2);
                }
            }
        }
    }

    public static void copyFile(File sourceFile, File targetFile)
            throws IOException {
        BufferedInputStream inBuff = null;
        BufferedOutputStream outBuff = null;
        if (null != sourceFile && null != targetFile) {
            try {
                inBuff = new BufferedInputStream(new FileInputStream(
                        sourceFile));
                outBuff = new BufferedOutputStream(new FileOutputStream(
                        targetFile));
                byte[] buffer = new byte[BUFFER];
                int length;
                while ((length = inBuff.read(buffer)) != -1) {
                    outBuff.write(buffer, 0, length);
                }
                outBuff.flush();
            } finally {
                if (inBuff != null) {
                    inBuff.close();
                }
                if (outBuff != null) {
                    outBuff.close();
                }
            }
        }
    }
}

Related

  1. copyDirectory(File from, File to)
  2. copyDirectory(File from, File to, byte[] buffer)
  3. copyDirectory(File source, File destination)
  4. copyDirectory(String from, String to)
  5. copyDirectory(String sourceDirName, String destinationDirName)