Java FileInputStream Copy copyFile(File aSource, File aDest)

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

Description

Copies a file from one location to another.

License

Open Source License

Declaration

public static File copyFile(File aSource, File aDest) throws IOException 

Method Source Code


//package com.java2s;
import java.io.*;

public class Main {
    /**//from ww  w.j  a v a2  s  .c  om
     * Copies a file from one location to another.
     */
    public static File copyFile(File aSource, File aDest) throws IOException {
        // Get input stream, output file and output stream
        FileInputStream fis = new FileInputStream(aSource);
        File out = aDest.isDirectory() ? new File(aDest, aSource.getName()) : aDest;
        FileOutputStream fos = new FileOutputStream(out);

        // Iterate over read/write until all bytes written
        byte[] buf = new byte[8192];
        for (int i = fis.read(buf); i != -1; i = fis.read(buf))
            fos.write(buf, 0, i);

        // Close in/out streams and return out file
        fis.close();
        fos.close();
        return out;
    }
}

Related

  1. copyFile(File base, String relfilepath, File targetdir)
  2. copyFile(File copyFrom, File copyTo)
  3. copyFile(File copyFrom, File copyTo)
  4. copyFile(File destfile, File srcfile)