Java FileInputStream Copy copyFile(File src, File dest)

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

Description

Copies src File to dest directory (preserve's src filename: src.getName()), using Input/Output streams.

License

BSD License

Parameter

Parameter Description
src the source file
dest the destination directory

Exception

Parameter Description
IOException an exception

Declaration

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

Method Source Code

//package com.java2s;
/*L//from  ww  w.java  2 s  .  co m
 * Copyright The General Hospital Corporation d/b/a Massachusetts General Hospital
 *
 * Distributed under the OSI-approved BSD 3-Clause License.
 * See http://ncip.github.com/digital-model-repository/LICENSE.txt for details.
 */

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 src File to dest directory (preserve's src filename:
     * src.getName()), using Input/Output streams.
     * 
     * @param src
     *            the source file
     * @param dest
     * 
     *            the destination directory
     * @throws IOException
     */
    public static void copyFile(File src, File dest) throws IOException {
        FileInputStream in = new FileInputStream(src);
        if (dest.isDirectory())
            dest = new File(dest, src.getName());
        FileOutputStream out = new FileOutputStream(dest);
        copyInputStream(in, out);
        in.close();
        out.close();
    }

    private static final void copyInputStream(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[1024];
        int len;

        while ((len = in.read(buffer)) >= 0)
            out.write(buffer, 0, len);

        in.close();
        out.close();
    }
}

Related

  1. copyFile(File sourceLocation, File targetLocation)
  2. copyFile(File src, File dest)
  3. copyFile(File src, File dest)
  4. copyFile(File src, File dest)
  5. copyFile(File src, File dest)
  6. copyFile(File src, File dest)
  7. copyFile(File src, File dest)
  8. copyFile(File src, File dest)
  9. copyFile(File src, File dest)