Java Directory Copy copyDirectory(File sourceLocation, File targetLocation, int bufferSize)

Here you can find the source of copyDirectory(File sourceLocation, File targetLocation, int bufferSize)

Description

Copy directory.

License

Apache License

Parameter

Parameter Description
sourceLocation the source location
targetLocation the target location
bufferSize a parameter

Exception

Parameter Description
IOException Signals that an I/O exception has occurred.

Declaration

public static void copyDirectory(File sourceLocation, File targetLocation, int bufferSize) throws IOException 

Method Source Code


//package com.java2s;
/*//ww  w . j a v  a2s .c  om
 * Copyright 2004, 2009 The Apache Software Foundation.
 *
 * 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;
import java.io.OutputStream;

public class Main {
    /**
     * Copy directory.
     * 
     * @param sourceLocation
     *            the source location
     * @param targetLocation
     *            the target location
     * @param bufferSize 
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     */
    public static void copyDirectory(File sourceLocation, File targetLocation, int bufferSize) throws IOException {

        if (sourceLocation.isDirectory()) {
            if (!targetLocation.exists()) {
                targetLocation.mkdir();
            }

            String[] children = sourceLocation.list();
            for (int i = 0; i < children.length; i++) {
                copyDirectory(new File(sourceLocation, children[i]), new File(targetLocation, children[i]),
                        bufferSize);
            }
        } else {

            InputStream in = new FileInputStream(sourceLocation);
            OutputStream out = new FileOutputStream(targetLocation);

            // Copy the bits from instream to outstream
            byte[] buf = new byte[bufferSize];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            in.close();
            out.close();
        }
    }
}

Related

  1. copyDirectory(File sourceLocation, File targetLocation)
  2. copyDirectory(File sourceLocation, File targetLocation)
  3. copyDirectory(File sourceLocation, File targetLocation)
  4. copyDirectory(File sourceLocation, File targetLocation)
  5. copyDirectory(File sourceLocation, File targetLocation, boolean overwrite)
  6. copyDirectory(File sourceLocation, File targetLocation, String[] fileExtensions)