Java FileInputStream Copy copyFile(File source, File dest)

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

Description

Copies the source file to the dest file

License

Open Source License

Parameter

Parameter Description
source a parameter
dest a parameter

Declaration

public static void copyFile(File source, File dest) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010 Conselleria de Infraestructuras y Transporte, Generalitat 
 * de la Comunitat Valenciana . 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
 * //  w w  w  .  j av a 2 s.  c  o m
 * Contributors: H?ctor Iturria (Prodevelop) ? Initial 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 the source file to the dest file
     * 
     * @param source
     * @param dest
     */
    public static void copyFile(File source, File dest) {
        InputStream in = null;
        OutputStream out = null;

        try {
            if (!dest.exists()) {
                dest.createNewFile();
            }
            in = new FileInputStream(source);
            out = new FileOutputStream(dest);
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
        } catch (IOException e) {
            System.out.print("Something went wrong while copying the file: " + e.getMessage());
        } finally {
            try {
                in.close();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Related

  1. copyFile(File original, File destination)
  2. copyFile(File original, File parent)
  3. copyFile(File originalFile, File destinationDir)
  4. copyFile(File pSrc, File pDst, boolean pForce)
  5. copyFile(File source, File dest)
  6. copyFile(File source, File dest)
  7. copyFile(File source, File dest)
  8. copyFile(File source, File dest)
  9. copyFile(File source, File dest)