Java FileInputStream Copy copyFile(File source, File destination)

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

Description

This method copies a single file.

License

Open Source License

Parameter

Parameter Description
source the source
destination the destination

Exception

Parameter Description
IOException copy problem

Declaration

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

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2008-2011 Chair for Applied Software Engineering,
 * Technische Universitaet Muenchen.//  www.ja  v  a  2  s  .c o m
 * 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:
 ******************************************************************************/

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class Main {
    /**
     * This method copies a single file.
     * 
     * @param source the source
     * @param destination the destination
     * @throws IOException copy problem
     */
    public static void copyFile(File source, File destination) throws IOException {
        copyFile(new FileInputStream(source), destination);
    }

    /**
     * This method copies a single file.
     * 
     * @param source the source input stream
     * @param destination the destination
     * @throws IOException copy problem
     */
    public static void copyFile(InputStream source, File destination) throws IOException {
        if (source == null || destination == null) {
            throw new IOException("Source or destination is null.");
        }

        if (destination.getParentFile() != null) {
            destination.getParentFile().mkdirs();
        }
        FileOutputStream outputStream = new FileOutputStream(destination);

        byte[] buffer = new byte[4096];
        int read;
        while ((read = source.read(buffer)) != -1) {
            outputStream.write(buffer, 0, read);
        }

        source.close();
        outputStream.close();
    }
}

Related

  1. copyFile(File source, File destination)
  2. copyFile(File source, File destination)
  3. copyFile(File source, File destination)
  4. copyFile(File source, File destination)
  5. copyFile(File source, File destination)
  6. copyFile(File source, File destination)
  7. copyFile(File source, File destination)
  8. copyFile(File source, File target)
  9. copyFile(File source, File target)