Java Directory Copy nio copyDirNIO(File sourceLocation, File targetLocation)

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

Description

Copy directory.

License

Open Source License

Parameter

Parameter Description
sourceLocation the source location
targetLocation the target location

Exception

Parameter Description
IOException Signals that an I/O exception has occurred.

Declaration

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

Method Source Code

//package com.java2s;
/*/*w  ww .  j av a2  s . c  o m*/
 * Copyright (c) 2006-2010 Nokia Corporation and/or its subsidiary(-ies). 
 * All rights reserved.
 * This component and the accompanying materials are made available
 * under the terms of "Eclipse Public License v1.0"
 * which accompanies this distribution, and is available
 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
 *
 * Initial Contributors:
 * Nokia Corporation - initial contribution.
 *
 * Contributors:
 *
 * Description:
 *
 */

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

import java.io.IOException;
import java.io.InputStream;

import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.io.Reader;
import java.io.Writer;

import java.nio.channels.FileChannel;

public class Main {
    /**
     * Copy directory.
     * 
     * @param sourceLocation
     *            the source location
     * @param targetLocation
     *            the target location
     * 
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     */
    public static void copyDirNIO(File sourceLocation, File targetLocation)
            throws IOException {
        if (sourceLocation.isDirectory()) {
            if (!targetLocation.exists()) {
                targetLocation.mkdir();
            }

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

    /**
     * Copy using nio.
     * 
     * @param from
     *            the from
     * @param to
     *            the to
     * 
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     */
    public static void copyUsingNIO(String src, String dest)
            throws IOException {
        File sourceFile = new File(src);
        File destFile = new File(dest);

        copyUsingNIO(sourceFile, destFile);
    }

    /**
     * Copy using nio.
     * 
     * @param sourceFile
     *            the source file
     * @param destFile
     *            the dest file
     * 
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     */
    public static void copyUsingNIO(File sourceFile, File destFile)
            throws IOException {
        if (!sourceFile.exists()) {
            return;
        }
        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();
            }
        }
    }

    /**
     * Closes the streams and resources.
     * 
     * @param resource
     *            to be closed.
     */
    public static void close(Object resource) {
        try {
            if (resource instanceof InputStream) {
                ((InputStream) resource).close();
            } else if (resource instanceof OutputStream) {
                ((OutputStream) resource).close();
            } else if (resource instanceof Reader) {
                ((Reader) resource).close();
            } else if (resource instanceof Writer) {
                ((Writer) resource).close();
            } else if (resource instanceof RandomAccessFile) {
                ((RandomAccessFile) resource).close();
            } else if (resource != null) {
                throw new IllegalArgumentException("Unknown resource: "
                        + resource);
            }
        } catch (IOException e) {
        }
    }
}

Related

  1. copyDirectory(Path source, ArrayList targets)
  2. copyDirectory(String fromPath, String toPath)
  3. copyDirectory(String source, String destination)
  4. copyDirectoryRecursively(File source, File target)
  5. copyDirectoryToDirectory(File srcDir, File destDir)