Java BufferedInputStream Copy copyFile(File source, File target)

Here you can find the source of copyFile(File source, File target)

Description

Copies a file from source to target.

License

Apache License

Parameter

Parameter Description
source Source file or directory
target Target file or directory

Exception

Parameter Description
IOException an exception

Declaration

public static void copyFile(File source, File target) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2009, 2010 Innovation Gate GmbH
 * /*w w w .ja va2 s  . c o m*/
 * 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.BufferedInputStream;
import java.io.BufferedOutputStream;

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 {
    /**
     * Copies a file from source to target.
     * This method also is able to copy complete directories. The following behaviour applies by condition of the parameter file types:<br>
     * <p>
     * <b>source is regular file. Target is regular file or nonexistent:</b><br>
     * Source file is copied to target file. Target is overwritten if it already exists.
     * </p>
     * <p>
     * <b>source is regular file. Target is directory:</b><br>
     * Source file is copied into target directory. Gets the same name as the source file.
     * </p>
     * <p>
     * <b>source is directory.. Target is directory or nonexistent:</b><br>
     * Complete source directory is copied into the target directory. The copied directory becomes a sub directory of the target which is created if it does not yet exist.
     * </p>
     * @param source Source file or directory
     * @param target Target file or directory
     * @throws IOException
     */
    public static void copyFile(File source, File target) throws IOException {

        if (source.isDirectory()) {
            File subtarget = new File(target, source.getName());
            copyDirContent(source, subtarget);
        } else {
            if (!target.exists()) {
                if (!target.createNewFile()) {
                    throw new IOException("Unable to create target file");
                }
            } else if (target.isDirectory()) {
                target = new File(target, source.getName());
            }

            byte[] buf = new byte[2048];
            InputStream in = new BufferedInputStream(new FileInputStream(source));
            OutputStream out = new BufferedOutputStream(new FileOutputStream(target));
            int len;
            while ((len = in.read(buf)) != -1) {
                out.write(buf, 0, len);
            }

            out.flush();
            in.close();
            out.close();
        }

    }

    /**
     * Variant of {@link #copyFile(File, File)} that takes file paths as argument
     * @param source File path of source file 
     * @param target File path of target file
     * @throws IOException
     */
    public static void copyFile(String source, String target) throws IOException {
        copyFile(new File(source), new File(target));
    }

    /**
     * Copies the contents of a directory into a target directory.
     * @param source The source directory
     * @param targetDir The target directory
     * @throws IOException
     */
    public static void copyDirContent(File source, File targetDir) throws IOException {

        if (!source.exists() || !source.isDirectory()) {
            throw new IOException("Source file is no directory: " + source.getPath());
        }

        if (!targetDir.exists()) {
            if (!targetDir.mkdirs()) {
                throw new IOException("Unable to create target directory " + targetDir.getPath());
            }
        } else if (!targetDir.isDirectory()) {
            throw new IOException("Cannot copy to directory " + targetDir.getPath()
                    + " because there already is a regular file of that name");
        }

        File[] files = source.listFiles();
        for (int i = 0; i < files.length; i++) {
            copyFile(files[i], targetDir);
        }
    }
}

Related

  1. copyFile(File source, File dest, boolean deleteIfExists)
  2. copyFile(File source, File destination)
  3. copyFile(File source, File destination)
  4. copyFile(File source, File destination, boolean overwrite)
  5. copyFile(File source, File target)
  6. copyFile(File source, File target)
  7. copyFile(File sourceFile, File targetFile)
  8. copyFile(File sourceFile, File targetFile)
  9. copyFile(File sourceFile, File targetFile)