Java FileInputStream Copy copyFile(File source, File dest)

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

Description

Copy a file (a "real" file, not directory).

License

Open Source License

Parameter

Parameter Description
source the source file
dest the destination file

Declaration

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

Method Source Code


//package com.java2s;
//License from project: Open Source License 

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 {
    private static final int DEF_BUF_SZ = 1024 * 4;

    /**/* w  w w  .  jav  a  2  s  .co m*/
     * Copy a file (a "real" file, not directory).  Details on
     * the "rules" as follows:
     * <br><br>
     * <ul>
     *   <li>
     *     If <code>source</code> or <code>dest</code> denote
     *     directories, an exception will be thrown
     *   </li>
     *   <li>
     *     If <code>dest</code> does not exist ant its parent
     *     directory(ies) do not exist, then the parent
     *     directories are implicitly created.  If this operation
     *     fails, an exception is thrown.  If the parent directories
     *     are implicitly created yet the copy fails, the newly
     *     created parents are <b>not</b> deleted (enhancement)
     *   </li>
     *   <li>
     *     If <code>source</code> does not exist, the
     *     method returns without error.
     *   </li>
     *   <li>
     *     If <code>source</code> is not readable, an exception
     *     is thrown.
     *   </li>
     *   <li>
     *     If <code>dest</code> exists and is not writable, an exception
     *     is thrown.
     *   </li>
     *   <li>
     *     If <code>dest</code> does not exists yet its parent
     *     directory is not writable, an exception is thrown.
     *   </li>
     *   <li>
     *     If <code>dest</code> exists, it will
     *     be overwritten unless it is not writable
     *     in which case an exception will be thrown.  If there
     *     is an error in overwritting the file, the original
     *     is clobbered and left in an indeterminate state
     *     (sorry - another enhancement).
     *   </li>
     * </ul>
     *
     * Note that if an exception is thrown, no open streams remain.
     *
     * @param source the source file
     * @param dest the destination file
     *
     * @exception IOException if something goes wrong from the file system
     */
    public static void copyFile(File source, File dest) throws IOException {

        //Check for blank source
        if (!source.exists()) {
            return;
        }

        //Readable
        if (!source.canRead()) {
            throw new IOException("No read permission for file \"" + source + "\"");
        }
        //Check for dirs
        if (source.isDirectory()) {
            throw new IOException("Source file \"" + source + " is a directory");
        }
        if (dest.isDirectory()) {
            throw new IOException("Destination file \"" + dest + " is a directory");
        }

        //Writable
        if (dest.exists() && !dest.canWrite()) {
            throw new IOException("No write permission for file \"" + dest + "\"");
        }

        //Create target parent, if required
        if (!dest.exists() && !dest.getParentFile().exists()) {
            if (!dest.getParentFile().mkdirs()) {
                throw new IOException("Unable to implicitly create parent " + "directory for destination \""
                        + dest.getParentFile() + "\"");
            }
        }

        FileInputStream fIn = null;
        FileOutputStream fOut = null;
        try {
            fIn = new FileInputStream(source);
            fOut = new FileOutputStream(dest);
            pipe(fIn, fOut);
            fOut.flush();
            close(fIn);
            close(fOut);
        } catch (Exception ex) {
            close(fIn);
            close(fOut);
            IOException newEx = new IOException("Unable to copy file");
            newEx.initCause(ex);
            throw newEx;
        }
    }

    /**
     * Pipe data from the input to the output stream
     * @param in The input stream
     * @param out The output stream
     * @return The amount of data piped
     * @throws IOException
     */
    public static long pipe(final InputStream in, final OutputStream out) throws IOException {
        return pipe(in, out, new byte[DEF_BUF_SZ], Long.MAX_VALUE);
    }

    /**
     * Pipe data from the input to the output stream
     * @param in The input stream
     * @param out The output stream
     * @param maxTransfer The maximum amount of data to transfer
     * @return The amount of data piped
     * @throws IOException
     */
    public static long pipe(final InputStream in, final OutputStream out, final long maxTransfer)
            throws IOException {
        return pipe(in, out, new byte[DEF_BUF_SZ], maxTransfer);
    }

    /**
     * Pipe data from the input to the output stream
     * @param in The input stream
     * @param out The output stream
     * @param transferBuf The transfer buffer
     * @return The amount of data piped
     * @throws IOException
     */
    public static long pipe(final InputStream in, final OutputStream out, final byte[] transferBuf)
            throws IOException {
        return pipe(in, out, transferBuf, Long.MAX_VALUE);
    }

    /**
     * Pipe the contents of <code>in</code> to <code>out</code>
     *
     * @param in the source
     * @param out the destination
     * @param transferBuf a pre-allocated byte buffer used
     *        to perform transfer
     * @param maxTransfer the maximum number of bytes to transfer
     *
     * @return the actual number of bytes transfered
     *
     * @exception IOException from the underlying streams.
     */
    public static long pipe(final InputStream in, final OutputStream out, final byte[] transferBuf,
            final long maxTransfer) throws IOException {
        final int bufLen = transferBuf.length;
        long ret = 0;

        while (ret < maxTransfer) {
            long amtToRead = maxTransfer - ret;
            if (amtToRead > bufLen) {
                amtToRead = bufLen;
            }
            int read = in.read(transferBuf, 0, (int) amtToRead);
            if (read == -1) {
                break;
            }
            out.write(transferBuf, 0, read);
            ret += read;
        }
        return ret;
    }

    /**
     * Safe close of an OutputStream (no exceptions,
     * even if reference is null).
     *
     * @param out the stream to close
     */
    public static void close(OutputStream out) {
        try {
            out.close();
        } catch (Exception ignore) {
        }
    }

    /**
     * Safe close of an InputStream (no exceptions,
     * even if reference is null).
     *
     * @param in the stream to close
     */
    public static void close(InputStream in) {
        try {
            in.close();
        } catch (Exception ignore) {
        }
    }
}

Related

  1. copyFile(File originalFile, File destinationDir)
  2. copyFile(File pSrc, File pDst, boolean pForce)
  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 dest)
  9. copyFile(File source, File dest)