Java FileChannel Copy copyTree(File srcFile, File trgFile)

Here you can find the source of copyTree(File srcFile, File trgFile)

Description

Copies a whole directory tree to another directory.

License

BSD License

Parameter

Parameter Description
srcFile the source tree
trgFile the target point where to copy the source tree

Exception

Parameter Description
IOException if an I/O error occurs

Declaration

public static void copyTree(File srcFile, File trgFile) throws IOException 

Method Source Code


//package com.java2s;
/*/* w  w w . j a v a  2 s  .c o m*/
 * Copyright (c) 2008-2011 by Bjoern Kolbeck, Jan Stender,
 *               Felix Langner, Zuse Institute Berlin
 *
 * Licensed under the BSD License, see LICENSE file for details.
 *
 */

import java.io.File;

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

public class Main {
    /**
     * Copies a whole directory tree to another directory.
     *
     * @param srcFile
     *            the source tree
     * @param trgFile
     *            the target point where to copy the source tree
     * @throws IOException
     *             if an I/O error occurs
     */
    public static void copyTree(File srcFile, File trgFile) throws IOException {

        if (srcFile.isDirectory()) {

            trgFile.mkdir();
            for (File file : srcFile.listFiles()) {
                copyTree(file, new File(trgFile, file.getName()));
            }
        } else {

            FileChannel in = null, out = null;
            try {
                in = new FileInputStream(srcFile).getChannel();
                out = new FileOutputStream(trgFile).getChannel();

                in.transferTo(0, in.size(), out);
            } finally {
                if (in != null)
                    in.close();
                if (out != null)
                    out.close();
            }
        }
    }
}

Related

  1. copyToFolderAs(File fromFile, File folder, String asName)
  2. copyToTemp(File srcFile)
  3. copyToTempDirectory(final String file)
  4. copyTransfer(String srcPath, String destPath)
  5. copyTree(File sourceLocation, File targetLocation)
  6. copyTree(final File source, final File dest)
  7. copyURLToFile(URL in, File out)
  8. copyUsingNIO(String src, String dest)
  9. copyWithChannels(File aSourceFile, File aTargetFile, boolean aAppend)