Java FileInputStream Copy copyFile(final InputStream in, final File destFile)

Here you can find the source of copyFile(final InputStream in, final File destFile)

Description

Copy a file.

License

Open Source License

Parameter

Parameter Description
in <code>InputStream</code>
destFile out as <code>File</code>

Exception

Parameter Description
IOException if file cannot ne copied

Declaration

public static void copyFile(final InputStream in, final File destFile)
        throws IOException 

Method Source Code

//package com.java2s;
/*/*from w  ww.j a  va 2  s .c o  m*/
 This file is part of the xframe software package
 hosted at http://xframe.sourceforge.net

 Copyright (c) 2003 Kurt Riede.

 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2.1 of the License, or (at your option) any later version.

 This library is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

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 {
    /** Size of buffer when copying files. */
    private static final int COPY_BUFFER_SIZE = 8192;

    /**
     * Copy a file.
     *
     * @param in <code>InputStream</code>
     * @param destFile out as <code>File</code>
     * @throws IOException if file cannot ne copied
     */
    public static void copyFile(final InputStream in, final File destFile)
            throws IOException {
        if (destFile.exists() && destFile.isFile()) {
            destFile.delete();
        }
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(destFile);
            copyFile(in, out);
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }

    /**
     * Copy a file.
     *
     * @param in <code>InputStream</code>
     * @param out as <code>OutputStream</code>
     * @throws IOException if file cannot ne copied
     */
    public static void copyFile(final InputStream in, final OutputStream out)
            throws IOException {
        final byte[] buffer = new byte[COPY_BUFFER_SIZE];
        int count = 0;
        do {
            out.write(buffer, 0, count);
            count = in.read(buffer, 0, buffer.length);
        } while (count != -1);
    }

    /**
     * Copy a file.
     *
     * @param in <code>InputStream</code>
     * @param destFile out file name
     * @throws IOException if file cannot ne copied
     */
    public static void copyFile(final InputStream in, final String destFile)
            throws IOException {
        copyFile(in, new File(destFile));
    }

    /**
     * Copy a file.
     *
     * @param inFile source file name
     * @param destFile destination file name
     * @throws IOException if file cannot ne copied
     */
    public static void copyFile(final String inFile, final String destFile)
            throws IOException {
        FileInputStream in = null;
        try {
            in = new FileInputStream(inFile);
            copyFile(in, destFile);
        } finally {
            if (in != null) {
                in.close();
            }
        }
    }
}

Related

  1. copyFile(final File source, final File target)
  2. copyFile(final File src, final File dest)
  3. copyFile(final File src, final File dst)
  4. copyFile(final File srcFile, final File destFile)
  5. copyFile(final File srcPath, final File dstPath)
  6. copyFile(final String from, final String to)
  7. copyFile(final String sourceFile, final String destinationFile)
  8. copyFile(final String sourceFilePath, final String destFilePath)
  9. copyFile(final String sSource, final String sDest)