Java File Copy nio copy(File src, File dst)

Here you can find the source of copy(File src, File dst)

Description

copy

License

Open Source License

Declaration

public static boolean copy(File src, File dst) 

Method Source Code


//package com.java2s;
/*/* w  ww  .j  ava2  s  .co  m*/
 * Axiom Stack Web Application Framework
 * Copyright (C) 2008  Axiom Software Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Axiom Software Inc., 11480 Commerce Park Drive, Third Floor, Reston, VA 20191 USA
 * email: info@axiomsoftwareinc.com
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

public class Main {
    public static boolean copy(File src, File dst) {
        boolean success = true;
        FileChannel srcChannel = null, dstChannel = null;
        try {
            srcChannel = new FileInputStream(src).getChannel();
            dstChannel = new FileOutputStream(dst).getChannel();
            dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
        } catch (IOException e) {
            success = false;
        } finally {
            if (srcChannel != null) {
                try {
                    srcChannel.close();
                } catch (IOException ignore) {
                }
                srcChannel = null;
            }
            if (dstChannel != null) {
                try {
                    dstChannel.close();
                } catch (IOException ignore) {
                }
                dstChannel = null;
            }
        }
        return success;
    }
}

Related

  1. copy(File src, File dest)
  2. copy(File src, File dist)
  3. copy(File src, File dst)
  4. copy(File src, File dst)
  5. copy(File src, File dst)
  6. copy(File src, File folder)
  7. copy(File src, File target)
  8. copy(File src, File target)
  9. copy(File srcDir, File relSrcFile, File destDir, File relDestFile)