Java Directory Copy copyDirectory(File sourceDir, File destinationDir)

Here you can find the source of copyDirectory(File sourceDir, File destinationDir)

Description

copy Directory

License

Apache License

Declaration

public static void copyDirectory(File sourceDir, File destinationDir) throws IOException 

Method Source Code

//package com.java2s;
/*//from  ww w  .  ja  va2  s  .c o  m
 * Copyright 2009 the original author or authors.
 * Copyright 2009 SorcerSoft.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.io.InputStream;

public class Main {
    public static void copyDirectory(File sourceDir, File destinationDir) throws IOException {

        //      System.out.println("sourceDir = " + sourceDir);
        //      System.out.println("destinationDir = " + destinationDir);

        if (!sourceDir.isDirectory())
            throw new IOException("sourceDir is not a directory: sourceDir = " + sourceDir);

        if (destinationDir.exists() && destinationDir.isFile())
            throw new IOException("destinationDir exists and is a file: destinationDir = " + destinationDir);

        // if dest dir exists copy sourceDir to sub-dir of dest dir
        if (destinationDir.exists()) {
            destinationDir = new File(destinationDir, sourceDir.getName());
            //         System.out.println("mod destinationDir = " + destinationDir);
        }

        for (File file : sourceDir.listFiles()) {
            System.out.println("file = " + file);
            if (file.isFile()) {
                //            System.out.println("\tcopying file = " + file + " to " + new File(destinationDir, file.getName()));
                copyFile(file, new File(destinationDir, file.getName()));
            } else {
                // is directory
                copyDirectory(file, destinationDir);
            }
        }
    }

    /**
     * A method that creates a copy of file at new file path
     * @param sourceFile Source file 
     * @param destinationFile Destination file 
     * @throws IOException
     * @see redirectInputStream2File
     */
    public static void copyFile(File sourceFile, File destinationFile) throws IOException {
        // If the destination file's parent directory does not exit, create it
        destinationFile.getParentFile().mkdirs();

        //  Creating a new input file stream that is connected to the source file
        InputStream is = new FileInputStream(sourceFile);
        try {
            // Attempting to redirect the input file stream of the source file to the destination file path 
            redirectInputStream2File(is, destinationFile);
        } catch (IOException e) {
            // Caught an IOException 

            // Check if the destination file exist
            if (destinationFile.exists()) {
                // If the destination file exist delete the file 
                destinationFile.delete();
            }
            // The input file stream is reset
            is.reset();
            // Making a second attempt to copy the source file 
            redirectInputStream2File(is, destinationFile);
            // If the redirection fails a second time an IO Exception is thrown
        }
    }

    /**
     * This method redirects an input stream to a file
     * 
     * @param is Input stream
     * @param file File
     * @throws IOException
     */
    public static void redirectInputStream2File(InputStream is, File file) throws IOException {
        int bufferSize = 4096;
        FileOutputStream fos = new FileOutputStream(file);
        byte[] buffer = new byte[bufferSize];
        int bytesRead;
        while ((bytesRead = is.read(buffer)) != -1) { // EOF
            fos.write(buffer, 0, bytesRead);
        }
        fos.close();
        is.close();
    }
}

Related

  1. copyDirectory(File source, File target)
  2. copyDirectory(File source, File target)
  3. copyDirectory(File source, File target)
  4. copyDirectory(File sourceDir, File destDir)
  5. copyDirectory(File sourceDir, File destDir)
  6. copyDirectory(File sourceLocation, File targetLocation)
  7. copyDirectory(File sourceLocation, File targetLocation)
  8. copyDirectory(File sourceLocation, File targetLocation)
  9. copyDirectory(File sourceLocation, File targetLocation)