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

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

Description

copy Folder

License

Apache License

Declaration

public static void copyFolder(File sourceFolder,
            File destinationFolder, List<String> blacklist)
            throws IOException 

Method Source Code

//package com.java2s;
/*/*w  w w .  jav  a  2  s.  c o m*/
 * This file is part of FTB Launcher.
 *
 * Copyright ? 2012-2014, 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;

import java.util.List;

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 {
        copyFolder(sourceFolder, destinationFolder, true, null);
    }

    public static void copyFolder(File sourceFolder,
            File destinationFolder, List<String> blacklist)
            throws IOException {
        copyFolder(sourceFolder, destinationFolder, true, blacklist);
    }

    public static void copyFolder(File sourceFolder,
            File destinationFolder, boolean overwrite) throws IOException {
        copyFolder(sourceFolder, destinationFolder, overwrite, null);
    }

    public static void copyFolder(File sourceFolder,
            File destinationFolder, boolean overwrite,
            List<String> blacklist) 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, overwrite, blacklist);
            }
        } else if (blacklist == null || !blacklist.contains(sourceFolder)) {
            copyFile(sourceFolder, destinationFolder, overwrite);
        }
    }

    /**
     * @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 {
        copyFile(sourceFile, destinationFile, true);
    }

    public static void copyFile(File sourceFile, File destinationFile,
            boolean overwrite) throws IOException {
        if (sourceFile.exists()) {
            if (!destinationFile.exists()) {
                destinationFile.getParentFile().mkdirs();
                destinationFile.createNewFile();
            } else if (!overwrite) {
                return;
            }
            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)
  3. copyFolder(File src, File dest)
  4. copyFolder(File src, File dest)
  5. copyFolder(File src, File dst, FilenameFilter filter)
  6. copyFolder(final File from, final File to)