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

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

Description

Copy the specified file or directory to the destination.

License

Apache License

Parameter

Parameter Description
src File object representing the source
dest File object representing the destination

Declaration

public static boolean copy(File src, File dest) 

Method Source Code

//package com.java2s;
/*// w  w w  .  j  a  v  a2 s  .  c om
 * Copyright 2015 Async-IO.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

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

import java.nio.channels.FileChannel;

public class Main {
    /**
     * Copy the specified file or directory to the destination.
     *
     * @param src  File object representing the source
     * @param dest File object representing the destination
     */
    public static boolean copy(File src, File dest) {

        boolean result = true;

        String files[] = null;
        if (src.isDirectory()) {
            files = src.list();
            result = dest.mkdir();
        } else {
            files = new String[1];
            files[0] = "";
        }
        if (files == null) {
            files = new String[0];
        }
        for (int i = 0; (i < files.length) && result; i++) {
            File fileSrc = new File(src, files[i]);
            File fileDest = new File(dest, files[i]);
            if (fileSrc.isDirectory()) {
                result = copy(fileSrc, fileDest);
            } else {
                FileChannel ic = null;
                FileChannel oc = null;
                try {
                    ic = (new FileInputStream(fileSrc)).getChannel();
                    oc = (new FileOutputStream(fileDest)).getChannel();
                    ic.transferTo(0, ic.size(), oc);
                } catch (IOException e) {

                    result = false;
                } finally {
                    if (ic != null) {
                        try {
                            ic.close();
                        } catch (IOException ignored) {
                        }
                    }
                    if (oc != null) {
                        try {
                            oc.close();
                        } catch (IOException ignored) {
                        }
                    }
                }
            }
        }
        return result;

    }
}

Related

  1. copy(File sourceFile, File destinationFile)
  2. copy(File sourceFile, File targetFile)
  3. copy(File src, File dest)
  4. copy(File src, File dest)
  5. copy(File src, File dest)
  6. copy(File src, File dist)
  7. copy(File src, File dst)
  8. copy(File src, File dst)
  9. copy(File src, File dst)