Java FileInputStream Copy copyFile(final File src, final File dest)

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

Description

Copies a file or directory from one location to another location on a local system.

Note: The destination location must exist, otherwise a IOException will be thrown.

License

Open Source License

Parameter

Parameter Description
src The Source File or Directory to be copied to the dest location.
dest The Destination location to copy the Source content to.

Exception

Parameter Description
IOException Thrown if the destination location doesn't exist or there write permissions for the destination.

Declaration

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

Method Source Code


//package com.java2s;

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 {
    /**//ww  w . j  a  va 2 s. c  o  m
     * Copies a file or directory from one location to another location on a local system.<br /><br /> Note: The destination location must
     * exist, otherwise a IOException will be thrown.
     *
     * @param src  The Source File or Directory to be copied to the dest location.
     * @param dest The Destination location to copy the Source content to.
     * @throws IOException Thrown if the destination location doesn't exist or there write permissions for the destination.
     */
    public static void copyFile(final File src, final File dest) throws IOException {
        if (src.isDirectory()) {
            // if the directory does not exist, create it
            if (!dest.exists()) {
                dest.mkdir();
            }

            // get all the directory contents
            final String files[] = src.list();

            // Copy all the folders/files in the directory
            for (final String file : files) {
                // construct the src and dest file structure
                final File srcFile = new File(src, file);
                final File destFile = new File(dest, file);
                // recursive copy
                copyFile(srcFile, destFile);
            }
        } else {
            // if its a file, then copy it
            final InputStream in = new FileInputStream(src);
            final OutputStream out = new FileOutputStream(dest);

            byte[] buffer = new byte[1024];

            int length;
            // copy the file contents
            while ((length = in.read(buffer)) > 0) {
                out.write(buffer, 0, length);
            }

            // Clean up
            in.close();
            out.close();
        }
    }
}

Related

  1. copyFile(final File fromFile, final File toFile)
  2. copyFile(final File in, final File out)
  3. copyFile(final File oldfile, final File newfile)
  4. copyFile(final File source, final File dest)
  5. copyFile(final File source, final File target)
  6. copyFile(final File src, final File dst)
  7. copyFile(final File srcFile, final File destFile)
  8. copyFile(final File srcPath, final File dstPath)
  9. copyFile(final InputStream in, final File destFile)