Java Directory Copy copyDirectory(File source, File destination)

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

Description

copy Directory

License

Open Source License

Declaration

public static void copyDirectory(File source, File destination) throws IOException 

Method Source Code

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

import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Main {
    public static void copyDirectory(File source, File destination) throws IOException {
        copyDirectory(source, destination, null);
    }/*from  ww  w  .  j av  a  2 s .c o m*/

    public static void copyDirectory(File source, File destination, FileFilter filter) throws IOException {
        File nextDirectory = new File(destination, source.getName());

        //
        // create the directory if necessary...
        //
        if (!nextDirectory.exists() && !nextDirectory.mkdirs()) {
            Object[] filler = { nextDirectory.getAbsolutePath() };
            String message = "DirCopyFailed";
            throw new IOException(message);
        }

        File[] files = source.listFiles();

        //
        // and then all the items below the directory...
        //
        for (int n = 0; n < files.length; ++n) {
            if (filter == null || filter.accept(files[n])) {
                if (files[n].isDirectory())
                    copyDirectory(files[n], nextDirectory, filter);

                else
                    copyFile(files[n], nextDirectory);
            }
        }
    }

    public static void copyFile(File source, File destination) throws IOException {
        //
        // if the destination is a dir, what we really want to do is create
        // a file with the same name in that dir
        //
        if (destination.isDirectory())
            destination = new File(destination, source.getName());

        FileInputStream input = new FileInputStream(source);
        copyFile(input, destination);
    }

    public static void copyFile(InputStream input, File destination) throws IOException {
        OutputStream output = null;

        output = new FileOutputStream(destination);

        byte[] buffer = new byte[1024];

        int bytesRead = input.read(buffer);

        while (bytesRead >= 0) {
            output.write(buffer, 0, bytesRead);
            bytesRead = input.read(buffer);
        }

        input.close();

        output.close();
    }
}

Related

  1. copyDirectory(File inDir, File outDir)
  2. copyDirectory(File inputFolder, File outputFolder)
  3. copyDirectory(File source, File destination)
  4. copyDirectory(File source, File destination)
  5. copyDirectory(File source, File destination)
  6. copyDirectory(File source, File destination, boolean overwrite)
  7. copyDirectory(File source, File destination, String endWith)
  8. copyDirectory(File source, File target)
  9. copyDirectory(File source, File target)