Java FileInputStream Copy copyFile(File source, File destination)

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

Description

copy File

License

Open Source License

Declaration

private static void copyFile(File source, File destination) throws FileNotFoundException, IOException 

Method Source Code

//package com.java2s;
/**//from w ww  .jav a  2s. c  om
 * Copyright (c) 2005-2007 Intalio inc.
 *
 * 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:
 * Intalio inc. - initial API and implementation
 */

import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Main {
    private static void copyFile(File source, File destination) throws FileNotFoundException, IOException {
        FileInputStream fis = new FileInputStream(source);
        if (destination.isDirectory()) {
            destination = new File(destination, source.getName());
        }
        FileOutputStream fos = new FileOutputStream(destination);
        try {
            copyStream(fis, fos);
        } finally {
            fis.close();
            fos.close();
        }
    }

    /**
     * Copies the contents of the <code>InputStream</code> into the <code>OutputStream</code>.
     */
    public static void copyStream(InputStream input, OutputStream output) {
        try {
            byte[] bytes = new byte[4096];
            int bytesRead = 0;
            while ((bytesRead = input.read(bytes)) >= 0) {
                output.write(bytes, 0, bytesRead);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * Close stream, ignoring any possible IOException.
     */
    public static void close(Closeable c) {
        try {
            if (c != null)
                c.close();
        } catch (IOException except) {
            // ignore
        }
    }
}

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 destination)
  9. copyFile(File source, File target)