Java Directory Copy copyDirectory(File source, File target)

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

Description

copy Directory

License

Apache License

Declaration

private static void copyDirectory(File source, File target) throws IOException 

Method Source Code


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

import java.io.*;

public class Main {
    private static void copyDirectory(File source, File target) throws IOException {
        if (!target.exists()) {
            target.mkdir();/* w w w.j a  va2  s . c om*/
        }

        for (String f : source.list()) {
            copy(new File(source, f), new File(target, f));
        }
    }

    public static boolean copy(File sourceLocation, File targetLocation) throws IOException {
        if (sourceLocation.exists() == false) {
            return false;
        }
        if (sourceLocation.isDirectory()) {
            copyDirectory(sourceLocation, targetLocation);
        } else {
            copyFile(sourceLocation, targetLocation);
        }
        return true;
    }

    private static void copyFile(File source, File target) throws IOException {
        try (InputStream in = new FileInputStream(source); OutputStream out = new FileOutputStream(target)) {
            byte[] buf = new byte[1024];
            int length;
            while ((length = in.read(buf)) > 0) {
                out.write(buf, 0, length);
            }
        }
    }
}

Related

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