Java Directory Copy nio copyDirectory(File sourceFile, File destFile)

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

Description

copy Directory

License

Open Source License

Declaration

private static void copyDirectory(File sourceFile, File destFile) throws IOException 

Method Source Code

//package com.java2s;
// are made available under the terms of the Eclipse Public License v1.0

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import java.nio.channels.FileChannel;

public class Main {
    private static void copyDirectory(File sourceFile, File destFile) throws IOException {
        if (sourceFile.isDirectory()) {
            if (!destFile.exists()) {
                destFile.mkdir();/*from  w  w  w. ja  v a  2 s  .c o  m*/
            }
            String files[] = sourceFile.list();
            for (int i = 0; i < files.length; i++) {
                copyDirectory(new File(sourceFile, files[i]), new File(destFile, files[i]));
            }
        } else {
            if (!sourceFile.exists()) {
                //humf
            } else {

                copyFile(sourceFile, destFile);
            }
        }
    }

    private static void copyFile(File sourceFile, File destFile) throws IOException {
        if (!destFile.exists()) {
            destFile.createNewFile();
        }

        FileChannel source = null;
        FileChannel destination = null;
        try {
            source = new FileInputStream(sourceFile).getChannel();
            destination = new FileOutputStream(destFile).getChannel();
            destination.transferFrom(source, 0, source.size());
        } finally {
            if (source != null) {
                source.close();
            }
            if (destination != null) {
                destination.close();
            }
        }
    }
}

Related

  1. copyDirectory(File source, File destination, boolean recursive)
  2. copyDirectory(File source, File target, boolean acceptFileInUseError)
  3. copyDirectory(File sourceDir, File destinationDir)
  4. copyDirectory(File sourceDir, File targetDir)
  5. copyDirectory(File sourceDirectory, File targetDirectory)
  6. copyDirectory(File src, File dest)
  7. copyDirectory(File src, File target)
  8. copyDirectory(File srcDir, File destDir)
  9. copyDirectory(File srcDir, File destDir, boolean preserveFileDate)