Java Folder Copy nio copyFolder(File sourceFolder, File destinationFolder)

Here you can find the source of copyFolder(File sourceFolder, File destinationFolder)

Description

copy Folder

License

Apache License

Parameter

Parameter Description
sourceFolder - the folder to be moved
destinationFolder - where to move to

Exception

Parameter Description
IOException an exception

Declaration

public static void copyFolder(File sourceFolder, File destinationFolder) throws IOException 

Method Source Code


//package com.java2s;
/*//from   w  ww . j ava2 s  . c om
 * This file is part of FTB Launcher.
 *
 * Copyright ? 2012-2013, FTB Launcher Contributors <https://github.com/Slowpoke101/FTBLaunch/>
 * FTB Launcher is 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 {
    /**
     * @param sourceFolder - the folder to be moved
     * @param destinationFolder - where to move to
     * @throws IOException
     */
    public static void copyFolder(File sourceFolder, File destinationFolder) throws IOException {
        if (sourceFolder.isDirectory()) {
            if (!destinationFolder.exists()) {
                destinationFolder.mkdirs();
            }
            String files[] = sourceFolder.list();
            for (String file : files) {
                File srcFile = new File(sourceFolder, file);
                File destFile = new File(destinationFolder, file);
                copyFolder(srcFile, destFile);
            }
        } else {
            copyFile(sourceFolder, destinationFolder);
        }
    }

    /**
     * @param sourceFile - the file to be moved
     * @param destinationFile - where to move to
     * @throws IOException
     */
    public static void copyFile(File sourceFile, File destinationFile) throws IOException {
        if (sourceFile.exists()) {
            if (!destinationFile.exists()) {
                destinationFile.createNewFile();
            }
            FileChannel sourceStream = null, destinationStream = null;
            try {
                sourceStream = new FileInputStream(sourceFile).getChannel();
                destinationStream = new FileOutputStream(destinationFile).getChannel();
                destinationStream.transferFrom(sourceStream, 0, sourceStream.size());
            } finally {
                if (sourceStream != null) {
                    sourceStream.close();
                }
                if (destinationStream != null) {
                    destinationStream.close();
                }
            }
        }
    }
}

Related

  1. copyFolder(File sourceFolder, File destinationFolder)
  2. copyFolder(File sourceFolder, File destinationFolder, List blacklist)
  3. copyFolder(File src, File dest)
  4. copyFolder(File src, File dest)
  5. copyFolder(File src, File dst, FilenameFilter filter)