Java Copy Directory copyDirectory(File srcDir, File dstDir)

Here you can find the source of copyDirectory(File srcDir, File dstDir)

Description

copy Directory

License

Apache License

Declaration

public static void copyDirectory(File srcDir, File dstDir) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void copyDirectory(File srcDir, File dstDir) throws IOException {
        if (srcDir.isDirectory()) {
            if (!dstDir.exists()) {
                dstDir.mkdirs();//from  ww w  .ja  v a 2s  .com
            }

            String[] children = srcDir.list();
            for (int i = 0; i < children.length; i++) {
                copyDirectory(new File(srcDir, children[i]), new File(dstDir, children[i]));
            }
        } else {
            copy(srcDir, dstDir);
        }
    }

    public static void copyDirectory(File srcPath, File dstPath, List<File> filesToBeCopied) throws IOException {
        List<String> pathStringList = getToStringList(filesToBeCopied);

        for (String string : pathStringList) {
            String path = string.substring(srcPath.getPath().length() + 1);
            File destFile = new File(dstPath, path);
            destFile.getParentFile().mkdirs();
            copy(new File(string), destFile);
        }

        //      for (String string : pathStringList) {
        //           System.out.println("Path String: "+string );
        //        }
        //      
        //      if (srcPath.isDirectory()) {
        //         if (!dstPath.exists()) {
        //            dstPath.mkdir();
        //         }
        //         String files[] = srcPath.list();
        //         for (int i = 0; i < files.length; i++) {
        //            copyDirectory(new File(srcPath, files[i]), new File(dstPath, files[i]),
        //                          filesToBeCopied);
        //         }
        //      } else {
        //         if (!pathStringList.contains(srcPath.getAbsolutePath()))
        //            return;
        //         if (!srcPath.exists()) {
        //            return;
        //         } else {
        //            copy(srcPath, dstPath);
        //         }
        //      }
    }

    public static void copy(File src, File dst) throws IOException {
        if (dst.getParentFile() != null && !dst.getParentFile().exists()) {
            dst.getParentFile().mkdirs();
        }
        InputStream in = new FileInputStream(src);
        OutputStream out = new FileOutputStream(dst);

        // Transfer bytes from in to out
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
        in.close();
        out.close();
    }

    private static List<String> getToStringList(List<File> fileList) {
        List<String> resultStrings = new ArrayList<String>();
        for (Object object : fileList) {
            resultStrings.add(object.toString());
        }
        return resultStrings;
    }
}

Related

  1. copyDirectory(File src, File dest)
  2. copyDirectory(File src, File dest, boolean force)
  3. copyDirectory(File src, File dst)
  4. copyDirectory(File srcDir, File destDir)
  5. copyDirectory(File srcDir, File dstDir)
  6. copyDirectory(File srcDir, File dstDir)
  7. copyDirectory(File srcDir, File dstDir)
  8. copyDirectory(File srcDir, File dstDir)