Java Directory Copy copyDirectory(File sourceLocation, File targetLocation)

Here you can find the source of copyDirectory(File sourceLocation, File targetLocation)

Description

copy Directory

License

Open Source License

Declaration

public static void copyDirectory(File sourceLocation, File targetLocation) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.*;

public class Main {
    public static void copyDirectory(File sourceLocation, File targetLocation) throws IOException {
        if (sourceLocation.isDirectory()) {
            if (!targetLocation.exists()) {
                targetLocation.mkdirs();
            }/*from ww  w.j  a v a2 s.com*/
            String[] children = sourceLocation.list();
            for (int i = 0; i < children.length; i++) {
                copyDirectory(new File(sourceLocation, children[i]), new File(targetLocation, children[i]));
            }
        } else {

            InputStream in = new FileInputStream(sourceLocation);
            OutputStream out = new FileOutputStream(targetLocation);

            // Copy the bits from instream to outstream
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            in.close();
            out.close();
        }
    }
}

Related

  1. copyDirectory(File source, File target)
  2. copyDirectory(File sourceDir, File destDir)
  3. copyDirectory(File sourceDir, File destDir)
  4. copyDirectory(File sourceDir, File destinationDir)
  5. copyDirectory(File sourceLocation, File targetLocation)
  6. copyDirectory(File sourceLocation, File targetLocation)
  7. copyDirectory(File sourceLocation, File targetLocation)
  8. copyDirectory(File sourceLocation, File targetLocation, boolean overwrite)
  9. copyDirectory(File sourceLocation, File targetLocation, int bufferSize)