Java File Copy copyFiles(File source, File destination)

Here you can find the source of copyFiles(File source, File destination)

Description

Copies the contents of one file to another.

License

Apache License

Parameter

Parameter Description
source the source file
destination the destination file

Exception

Parameter Description
IOException if an error occurs while copying

Declaration

public static void copyFiles(File source, File destination) throws IOException 

Method Source Code

//package com.java2s;
/*/*  w  w w. j  av a2  s .c  o  m*/
 * Copyright (C) 2007 Roland Krueger
 * 
 * Created on 26.03.2010
 * 
 * Author: Roland Krueger (www.rolandkrueger.info)
 * 
 * This file is part of RoKlib.
 * 
 * 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 {
    /**
     * Copies the contents of one file to another.
     * 
     * @param source
     *          the source file
     * @param destination
     *          the destination file
     * @throws IOException
     *           if an error occurs while copying
     */
    public static void copyFiles(File source, File destination) throws IOException {
        copyStreams(new FileInputStream(source), new FileOutputStream(destination));
    }

    /**
     * <p>
     * Copies the data from the given {@link InputStream} to the given {@link OutputStream}.
     * <p>
     * Both source and destination stream will be closed after the copy process.
     * 
     * @param source
     *          a byte data source
     * @param destination
     *          the target of the copy operation
     * @throws IOException
     *           if an error occurs while copying
     */
    public static void copyStreams(InputStream source, OutputStream destination) throws IOException {
        copyStreams(source, destination, true, true);
    }

    /**
     * <p>
     * Copies the data from the given {@link InputStream} to the given {@link OutputStream}. The two boolean parameters
     * let you define whether you want the source and destination streams to be closed after the operation.
     * 
     * @param source
     *          a byte data source
     * @param destination
     *          the target of the copy operation
     * @param closeSource
     *          source stream will be closed if <code>true</code>
     * @param closeDestination
     *          destination stream will be closed if <code>true</code>
     * @throws IOException
     *           if an error occurs while copying
     */
    public static void copyStreams(InputStream source, OutputStream destination, boolean closeSource,
            boolean closeDestination) throws IOException {
        byte[] buffer = new byte[4096];
        int read;
        while ((read = source.read(buffer)) != -1) {
            destination.write(buffer, 0, read);
        }
        destination.flush();
        if (closeDestination)
            destination.close();
        if (closeSource)
            source.close();
    }
}

Related

  1. copyFile(File source, File target)
  2. copyFile(File src, File dest, Component parentComponent, String message)
  3. copyFileFromFileSystem(String sourceFileName, File target)
  4. copyFiles(File file, File destPath)
  5. copyFiles(File fromDir, File toDir)
  6. copyFiles(File sourceFile, File targetDir)
  7. copyFiles(File src, File dest)
  8. copyFiles(File src, File dest)
  9. copyFiles(File src, File dest, boolean override)