Java FileChannel Copy copyTree(File sourceLocation, File targetLocation)

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

Description

copy Tree

License

Open Source License

Declaration

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

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010-2015 BSI Business Systems Integration AG.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://from   w  w w. j a v  a  2s  . c om
 *     BSI Business Systems Integration AG - initial API and implementation
 ******************************************************************************/

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

import java.nio.channels.FileChannel;

public class Main {
    private static final int KILO_BYTE = 1024;

    public static void copyTree(File sourceLocation, File targetLocation) throws IOException {
        if (sourceLocation.isDirectory()) {
            if (!targetLocation.exists()) {
                targetLocation.mkdirs();
                targetLocation.setLastModified(sourceLocation.lastModified());
            }

            String[] children = sourceLocation.list();
            for (int i = 0; i < children.length; i++) {
                copyTree(new File(sourceLocation, children[i]), new File(targetLocation, children[i]));
            }
        } else {
            copyFile(sourceLocation, targetLocation);
        }
    }

    /**
     * Copies one file to another. Source must exist and be readable. Cannot copy a directory to a file. Will not copy if
     * time stamps and file size match, will overwrite otherwise.
     *
     * @param source
     *          the source file
     * @param dest
     *          the destination file
     * @throws IOException
     *           if an error occurs during the copy operation
     */
    @SuppressWarnings("resource")
    public static void copyFile(File source, File dest) throws IOException {
        if (!source.exists()) {
            throw new FileNotFoundException(source.getAbsolutePath());
        }
        if (!source.canRead()) {
            throw new IOException("cannot read " + source);
        }

        if (dest.exists() && !dest.canWrite()) {
            throw new IOException("cannot write " + dest);
        }

        if (source.isDirectory()) {
            // source can not be a directory
            throw new IOException("source is a directory: " + source);
        }

        // source is a file
        if (dest.isDirectory()) {
            String sourceFileName = source.getName();
            copyFile(source, new File(dest, sourceFileName));
        }
        // both source and dest are files
        boolean needCopy = true;
        if (dest.exists()) {
            needCopy = (dest.length() != source.length()) || (dest.lastModified() != source.lastModified());
        }
        if (needCopy) {
            // Copies the file
            FileChannel input = null;
            FileChannel output = null;
            try {
                // magic number for Windows, 64Mb - 32Kb
                //
                int mbCount = 64;
                boolean done = false;
                // java.io.IOException: Insufficient system resources exist to complete
                // the requested service
                while (!done) {
                    input = new FileInputStream(source).getChannel();
                    if (!dest.exists()) {
                        dest.getParentFile().mkdirs();
                    }
                    output = new FileOutputStream(dest).getChannel();

                    try {
                        int maxCount = (mbCount * KILO_BYTE * KILO_BYTE) - (32 * KILO_BYTE);
                        long size = input.size();
                        long position = 0;
                        while (position < size) {
                            position += input.transferTo(position, maxCount, output);
                        }
                        done = true;
                    } catch (IOException ioXcp) {
                        if (ioXcp.getMessage().contains(
                                "Insufficient system resources exist to complete the requested service")) {
                            mbCount--;
                            if (mbCount == 0) {
                                done = true;
                            }
                            if (input != null) {
                                input.close();
                            }
                            if (output != null) {
                                output.close();
                            }
                        } else {
                            throw ioXcp;
                        }
                    }
                }
            } finally {
                if (input != null) {
                    input.close();
                }
                if (output != null) {
                    output.close();
                }
            }

            if (dest.exists() && source.exists()) {
                dest.setLastModified(source.lastModified());
            }
        }
    }
}

Related

  1. copyTo(File srcFile, File destFile)
  2. copyToFolderAs(File fromFile, File folder, String asName)
  3. copyToTemp(File srcFile)
  4. copyToTempDirectory(final String file)
  5. copyTransfer(String srcPath, String destPath)
  6. copyTree(File srcFile, File trgFile)
  7. copyTree(final File source, final File dest)
  8. copyURLToFile(URL in, File out)
  9. copyUsingNIO(String src, String dest)