Java File Copy nio copy(File srcfile, File destfile)

Here you can find the source of copy(File srcfile, File destfile)

Description

Copy a file or directory from one location to another

License

Open Source License

Parameter

Parameter Description
srcfile The file to copy
destfile The destination for the copied file

Exception

Parameter Description
Exception If an exception occured during the copy

Declaration

public static void copy(File srcfile, File destfile) throws Exception 

Method Source Code

//package com.java2s;
/*/*from  w  w  w  . j  av  a2 s.c  o  m*/
 * JBoss, Home of Professional Open Source.
 * Copyright 2007, Red Hat Middleware LLC, and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

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

import java.nio.channels.FileChannel;

public class Main {
    /**
     * Copy a file or directory from one location to another
     * @param srcfile The file to copy
     * @param destfile The destination for the copied file
     * @throws Exception If an exception occured during the copy
     */
    public static void copy(File srcfile, File destfile) throws Exception {
        if (srcfile.isDirectory()) {
            destfile.mkdirs();
            String[] files = srcfile.list();
            for (int i = 0; i < files.length; i++) {
                String newSrcFile = srcfile.getPath() + File.separator + files[i];
                String newDestFile = destfile.getPath() + File.separator + files[i];
                copy(new File(newSrcFile), new File(newDestFile));
            }
        } else if (srcfile.isFile()) {
            FileChannel inputChannel = new FileInputStream(srcfile).getChannel();
            FileChannel outputChannel = new FileOutputStream(destfile).getChannel();
            inputChannel.transferTo(0, inputChannel.size(), outputChannel);
            inputChannel.close();
            outputChannel.close();

        } else {
            throw new Exception("Invalid file : " + srcfile);
        }
    }
}

Related

  1. copy(File src, File dst)
  2. copy(File src, File folder)
  3. copy(File src, File target)
  4. copy(File src, File target)
  5. copy(File srcDir, File relSrcFile, File destDir, File relDestFile)
  6. copy(File srcFile, File dstFile)
  7. copy(File srcFileOrDir, File targetFileOrDir)
  8. copy(File toFile, File fromFile)
  9. copyFile(File f1, File f2)