Java FileInputStream Copy copyFile(File source, File target, boolean deleteSourceAfter)

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

Description

Copies a source file in a target file.

License

Open Source License

Parameter

Parameter Description
source a file or a directory <p> If source is a directory, only its content is copied in the target directory. </p>
target a file or a directory (must have the same type than the source: file or directory)
deleteSourceAfter true to delete the source file after the copy is done

Exception

Parameter Description
IOException an exception

Declaration

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

Method Source Code

//package com.java2s;
/******************************************************************************
 * Copyright (c) 2008-2013, Linagora/*from  w  w  w.j  a v  a  2s.com*/
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *       Linagora - initial API and implementation
 *******************************************************************************/

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 source file in a target file.
     *
     * @param source a file or a directory
     * <p>
     * If source is a directory, only its content is copied in the target directory.
     * </p>
     *
     * @param target a file or a directory (must have the same type than the source: file or directory)
     * @param deleteSourceAfter true to delete the source file after the copy is done
     * @throws IOException
     */
    public static void copyFile(File source, File target, boolean deleteSourceAfter) throws IOException {

        // Copy a directory
        if (source.isDirectory()) {
            if (!target.exists() && !target.mkdir())
                throw new IOException("Could not create the directory " + target.getAbsolutePath());
            else if (target.exists() && !target.isDirectory())
                throw new IOException(target + " already exists and is not a directory.");

            File[] children = source.listFiles();
            for (File sourceChild : children) {
                File destChild = new File(target, sourceChild.getName());
                copyFile(sourceChild, destChild, deleteSourceAfter);
            }
        }

        // Copy a file
        else {
            if (!target.exists() && !target.createNewFile())
                throw new IOException("Could not create the file " + target.getAbsolutePath());
            else if (target.exists() && !target.isFile())
                throw new IOException(target + " already exists and is not a file.");

            copyStream(source, target);
        }

        if (deleteSourceAfter)
            deleteFilesRecursively(source);
    }

    /**
     * Copies the content from in into os.
     * <p>
     * Neither <i>in</i> nor <i>os</i> are closed by this method.<br />
     * They must be explicitly closed after this method is called.
     * </p>
     *
     * @param in
     * @param os
     * @throws IOException
     */
    public static void copyStream(InputStream in, OutputStream os) throws IOException {

        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            os.write(buf, 0, len);
        }
    }

    /**
     * Copies the content from in into outputFile.
     * <p>
     * <i>in</i> is not closed by this method.<br />
     * It must be explicitly closed after this method is called.
     * </p>
     *
     * @param in
     * @param outputFile will be created if it does not exist
     * @throws IOException
     */
    public static void copyStream(InputStream in, File outputFile) throws IOException {

        if (!outputFile.exists() && !outputFile.createNewFile())
            throw new IOException("Failed to create " + outputFile.getAbsolutePath() + ".");

        OutputStream os = new FileOutputStream(outputFile);
        copyStream(in, os);
        os.close();
    }

    /**
     * Copies the content from inputFile into outputFile.
     *
     * @param inputFile
     * @param outputFile will be created if it does not exist
     * @throws IOException
     */
    public static void copyStream(File inputFile, File outputFile) throws IOException {

        InputStream is = new FileInputStream(inputFile);
        copyStream(is, outputFile);
        is.close();
    }

    /**
     * Copies the content from inputFile into an output stream.
     *
     * @param inputFile
     * @param os the output stream
     * @throws IOException
     */
    public static void copyStream(File inputFile, OutputStream os) throws IOException {

        InputStream is = new FileInputStream(inputFile);
        copyStream(is, os);
        is.close();
    }

    /**
     * Deletes files recursively.
     * @param files the files to delete
     * @throws IOException if a file could not be deleted
     */
    public static void deleteFilesRecursively(File... files) throws IOException {

        if (files == null)
            return;

        for (File file : files) {
            if (file.exists()) {
                if (file.isDirectory())
                    deleteFilesRecursively(file.listFiles());

                if (!file.delete())
                    throw new IOException(file.getAbsolutePath() + " could not be deleted.");
            }
        }
    }
}

Related

  1. copyFile(File source, File target)
  2. copyFile(File source, File target)
  3. copyFile(File source, File target)
  4. copyFile(File source, File target)
  5. copyFile(File source, File target, boolean createParents, FileFilter filter)
  6. copyFile(File source, File target, boolean replaceIfExists)
  7. copyFile(File sourceDir, File destDir, String filename)
  8. copyFile(File sourceFile, File destDir)
  9. copyFile(File sourceFile, File destFile)