Java File Copy nio copy(File source, File destination)

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

Description

copy

License

Apache License

Declaration

public static void copy(File source, File destination) throws IOException 

Method Source Code

//package com.java2s;
/**//from   ww  w  .ja  va 2  s  .  co  m
 * Copyright 2014 Joshua Asbury (@theoriginalbit)
 *
 * 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 {
    public static void copy(File source, File destination) throws IOException {
        if (source.isDirectory()) {
            copyDirectory(source, destination);
        } else {
            copyFile(source, destination);
        }
    }

    /**
     * Recursively copies a directory from source to destination including
     * all the files within the folders
     */
    public static void copyDirectory(File source, File destination) throws IOException {
        if (!source.isDirectory()) {
            throw new IllegalArgumentException("Source (" + source.getPath() + ") must be a directory.");
        }

        if (!source.exists()) {
            throw new IllegalArgumentException("Source directory (" + source.getPath() + ") doesn't exist.");
        }

        if (destination.mkdirs()) {
            File[] files = source.listFiles();

            if (files != null) {
                for (File file : files) {
                    if (file.isDirectory()) {
                        copyDirectory(file, new File(destination, file.getName()));
                    } else {
                        String name = file.getName();
                        // allows for .lua extensions in development env, but removes for play env
                        if (name.endsWith(".lua")) {
                            name = name.substring(0, name.length() - 4);
                        }
                        copyFile(file, new File(destination, name));
                    }
                }
            }
        }
    }

    /**
     * Copies a file from source to destination
     */
    private static void copyFile(File source, File destination) throws IOException {
        FileInputStream inputStream = new FileInputStream(source);
        FileOutputStream outputStream = new FileOutputStream(destination);
        FileChannel sourceChannel = inputStream.getChannel();
        FileChannel targetChannel = outputStream.getChannel();
        sourceChannel.transferTo(0, sourceChannel.size(), targetChannel);
        sourceChannel.close();
        targetChannel.close();
        inputStream.close();
        outputStream.close();
    }
}

Related

  1. copy(File source, File dest, boolean preserveTime)
  2. copy(File source, File destination)
  3. copy(File source, File destination)
  4. copy(File source, File destination)
  5. copy(File source, File destination)
  6. copy(File source, File destination)
  7. copy(File source, File target)
  8. copy(File source, File target)
  9. copy(File source, File target)