Java FileInputStream Copy copyFile(File source, File dest)

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

Description

Copy a normal file to another.

License

Open Source License

Parameter

Parameter Description
source a parameter
dest destination directory or destination file

Exception

Parameter Description
IOException an exception

Declaration

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

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2008(c) The OBiBa Consortium. All rights reserved.
 * /*from ww  w  . j  ava 2 s  .com*/
 * This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/

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 {
    /**
     * Copy a normal file to another.
     * @param source
     * @param dest destination directory or destination file
     * @throws IOException
     */
    public static void copyFile(File source, File dest) throws IOException {
        if (source == null)
            throw new IllegalArgumentException("source cannot be null");
        if (dest == null)
            throw new IllegalArgumentException("dest cannot be null");

        if (dest.isDirectory()) {
            dest = new File(dest, source.getName());
        }

        if (!dest.exists()) {
            if (dest.createNewFile() == false) {
                throw new IOException("Cannot create destination file " + dest.getAbsolutePath());
            }
        }

        InputStream in = null;
        OutputStream out = null;

        try {
            in = new FileInputStream(source);
            out = new FileOutputStream(dest);

            byte[] buf = new byte[4096];
            int len;

            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
        } finally {
            try {
                if (in != null)
                    in.close();
            } catch (IOException e) {
                // ignore
            }
            try {
                if (out != null)
                    out.close();
            } catch (IOException e) {
                // ignore
            }
        }
    }
}

Related

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