Java FileChannel Copy copyTree(final File source, final File dest)

Here you can find the source of copyTree(final File source, final File dest)

Description

This method is used to copy an entire file tree from one location to another.

License

Open Source License

Parameter

Parameter Description
source The source directory to be copied.
dest The destination directory to receive the contents of the copy.

Exception

Parameter Description
IOException if the copy fails

Declaration

public static void copyTree(final File source, final File dest) throws IOException 

Method Source Code


//package com.java2s;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import java.nio.channels.FileChannel;

public class Main {
    /**//from   w  ww. java 2 s .c o m
     * This method is used to copy an entire file tree from one location to another. This is a deep copy.
     * 
     * @param source
     *            The source directory to be copied.
     * @param dest
     *            The destination directory to receive the contents of the copy.
     * @throws IOException
     *             if the copy fails
     */
    public static void copyTree(final File source, final File dest) throws IOException {
        if (source != null && dest != null && source.isDirectory() && dest.isDirectory()) {
            String[] children = source.list();
            for (String child : children) {
                File in = new File(source, child);
                File out = new File(dest, child);
                if (in.isDirectory()) {
                    out.mkdir();
                    copyTree(in, out);
                } else {
                    copyFile(in, out);
                }
            }
        }
    }

    /**
     * This method can be used to copy a file from one place to another.
     * 
     * @param input
     *            The file to be read
     * @param output
     *            The file to be written to
     * @throws IOException
     *             if anything fails.
     */
    public static void copyFile(final File input, final File output) throws IOException {
        FileInputStream in = null;
        FileOutputStream out = null;

        try {
            in = new FileInputStream(input);
            out = new FileOutputStream(output);
            FileChannel inChannel = in.getChannel();
            FileChannel outChannel = out.getChannel();
            long size = inChannel.size();
            inChannel.transferTo(0L, size, outChannel);
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException e) {
                /* ignore */
            }
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                /* ignore */
            }
        }
    }
}

Related

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