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

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

Description

copy

License

Open Source License

Declaration

public static long copy(File src, File target) throws IOException 

Method Source Code


//package com.java2s;
/*/*from w  w  w .  ja  va 2s.c om*/
 * Copyright (c) 2010-2014 Sonatype, Inc. All rights reserved.
 *
 * This program is licensed to you under the Apache License Version 2.0,
 * and you may not use this file except in compliance with the Apache License Version 2.0.
 * You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the Apache License Version 2.0 is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
 */

import java.io.Closeable;
import java.io.File;

import java.io.IOException;
import java.io.RandomAccessFile;

import java.nio.channels.FileChannel;

public class Main {
    public static long copy(File src, File target) throws IOException {
        RandomAccessFile in = null;
        RandomAccessFile out = null;
        FileChannel inChannel = null;
        FileChannel outChannel = null;
        try {
            in = new RandomAccessFile(src, "r");

            target.getParentFile().mkdirs();

            out = new RandomAccessFile(target, "rw");

            out.setLength(0);
            long size = in.length();

            // copy large files in chunks to not run into Java Bug 4643189
            // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4643189
            // use even smaller chunks to work around bug with SMB shares
            // http://forums.sun.com/thread.jspa?threadID=439695
            long chunk = (64 * 1024 * 1024) - (32 * 1024);

            inChannel = in.getChannel();
            outChannel = out.getChannel();

            int total = 0;
            do {
                total += inChannel.transferTo(total, chunk, outChannel);
            } while (total < size);
            return total;
        } finally {
            close(inChannel);
            close(outChannel);
            close(in);
            close(out);
        }
    }

    public static boolean mkdirs(File directory) {
        if (directory == null) {
            return false;
        }

        if (directory.exists()) {
            return false;
        }
        if (directory.mkdir()) {
            return true;
        }

        File canonDir = null;
        try {
            canonDir = directory.getCanonicalFile();
        } catch (IOException e) {
            return false;
        }

        File parentDir = canonDir.getParentFile();
        return (parentDir != null && (mkdirs(parentDir) || parentDir.exists()) && canonDir.mkdir());
    }

    private static void close(Closeable c) throws IOException {
        if (c != null) {
            c.close();
        }
    }
}

Related

  1. copy(File src, File dst)
  2. copy(File src, File dst)
  3. copy(File src, File dst)
  4. copy(File src, File dst)
  5. copy(File src, File folder)
  6. copy(File src, File target)
  7. copy(File srcDir, File relSrcFile, File destDir, File relDestFile)
  8. copy(File srcfile, File destfile)
  9. copy(File srcFile, File dstFile)