Java FileChannel Copy copy(final File aCopyFrom, final File aCopyTo)

Here you can find the source of copy(final File aCopyFrom, final File aCopyTo)

Description

This is a copy of the Utils package copy.

License

Open Source License

Parameter

Parameter Description
aCopyFrom a parameter
aCopyTo a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static final void copy(final File aCopyFrom, final File aCopyTo) throws IOException 

Method Source Code


//package com.java2s;
/*//from   w  w w.  jav  a 2 s  .  c  o  m
* Copyright (c) 2005-2009 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.nio.channels.FileChannel;
import java.util.logging.Logger;

public class Main {
    /**
     * This is a copy of the Utils package copy.
     * 
     * @param aCopyFrom
     * @param aCopyTo
     * @throws IOException
     */
    public static final void copy(final File aCopyFrom, final File aCopyTo) throws IOException {
        if (!aCopyTo.getParentFile().isDirectory() && !aCopyTo.getParentFile().mkdirs()) {
            throw new IOException("Could not create copy to directory: " + aCopyTo.getAbsolutePath());
        }

        FileChannel lSrcChannel = new FileInputStream(aCopyFrom).getChannel();
        FileChannel lDstChannel = new FileOutputStream(aCopyTo).getChannel();

        Logger.getAnonymousLogger().fine("\tCopying from \"" + aCopyFrom.getCanonicalPath() + "\" to \""
                + aCopyTo.getCanonicalPath() + "\"");

        lDstChannel.transferFrom(lSrcChannel, 0, lSrcChannel.size());

        lSrcChannel.close();
        lDstChannel.close();
    }
}

Related

  1. copy(FileInputStream in, FileOutputStream out)
  2. copy(FileInputStream inputStream, FileOutputStream outputStream)
  3. copy(FileInputStream iStream, FileOutputStream oStream)
  4. copy(final File fromFile, final File toFile)
  5. copy(final File source, final File dest)
  6. copy(final File src, File dst, final boolean overwrite)
  7. copy(final String aSrcPath, final String aDestPath)