Java FileChannel Copy copyUsingNIO(String src, String dest)

Here you can find the source of copyUsingNIO(String src, String dest)

Description

Copy using nio.

License

Open Source License

Parameter

Parameter Description
from the from
to the to

Exception

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

Declaration

public static void copyUsingNIO(String src, String dest)
        throws IOException 

Method Source Code

//package com.java2s;
/*/* ww w . j a v  a 2  s .com*/
 * 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 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. copyTransfer(String srcPath, String destPath)
  2. copyTree(File sourceLocation, File targetLocation)
  3. copyTree(File srcFile, File trgFile)
  4. copyTree(final File source, final File dest)
  5. copyURLToFile(URL in, File out)
  6. copyWithChannels(File aSourceFile, File aTargetFile, boolean aAppend)
  7. directoryCopy(URL source, URL destination)
  8. doCopyDirectory(File srcDir, File destDir, boolean preserveFileDate, List exclusionList)
  9. doCopyFile(File srcFile, File destFile, boolean preserveFileDate)