Java Copy Directory copyDirectory(File srcDir, File dstDir)

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

Description

copy Directory

License

Open Source License

Declaration

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

Method Source Code

//package com.java2s;
//License from project: Open Source 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.zip.CRC32;

public class Main {
    private static final String JPEG = "jpeg";
    private static final String JPG = "jpg";
    private static final int BUFFER_SIZE = 4 * 1024;
    private static final boolean CLOCK = true;
    private static final boolean VERIFY = true;

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

        if (".svn".equals(srcDir.getName())) {
            return;
        }// ww w . j a  v  a 2 s  . c o  m

        if (srcDir.isDirectory()) {
            if (!dstDir.exists()) {
                dstDir.mkdir();
            }

            for (String aChildren : srcDir.list()) {
                copyDirectory(new File(srcDir, aChildren), new File(dstDir, aChildren));
            }
        } else {
            copyFile(srcDir, dstDir);
        }
    }

    public static boolean isDirectory(File f) {
        if (f != null && f.exists() && f.isDirectory()) {
            return true;
        }
        return false;
    }

    public static void copyFile(File srcFile, File destFile) throws IOException {
        if (!srcFile.getPath().toLowerCase().endsWith(JPG) && !srcFile.getPath().toLowerCase().endsWith(JPEG)) {
            return;
        }
        final InputStream in = new FileInputStream(srcFile);
        final OutputStream out = new FileOutputStream(destFile);
        try {
            long millis = System.currentTimeMillis();
            CRC32 checksum;
            if (VERIFY) {
                checksum = new CRC32();
                checksum.reset();
            }
            final byte[] buffer = new byte[BUFFER_SIZE];
            int bytesRead = in.read(buffer);
            while (bytesRead >= 0) {
                if (VERIFY) {
                    checksum.update(buffer, 0, bytesRead);
                }
                out.write(buffer, 0, bytesRead);
                bytesRead = in.read(buffer);
            }
            if (CLOCK) {
                millis = System.currentTimeMillis() - millis;
            }
        } catch (IOException e) {
            throw e;
        } finally {
            out.close();
            in.close();
        }
    }
}

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)
  9. copyDirectory(File srcDir, File dstDir)