Java FileInputStream Copy copyFile(File inputFile, File outputFile)

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

Description

Copies the content from inputFile into outputFile.

License

Open Source License

Parameter

Parameter Description
inputFile a parameter
outputFile will be created if it does not exist

Exception

Parameter Description
IOException an exception

Declaration

private static void copyFile(File inputFile, File outputFile) throws IOException 

Method Source Code

//package com.java2s;
/******************************************************************************
 * Copyright (c) 2008-2013, Linagora/*from   ww  w  .j  a v a  2s.  c  o m*/
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *       Linagora - initial API and implementation
 *******************************************************************************/

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 the content from inputFile into outputFile.
     *
     * @param inputFile
     * @param outputFile will be created if it does not exist
     * @throws IOException
     */
    private static void copyFile(File inputFile, File outputFile) throws IOException {

        InputStream is = null;
        OutputStream os = null;
        try {
            is = new FileInputStream(inputFile);
            if (!outputFile.exists() && !outputFile.createNewFile())
                throw new IOException("Failed to create " + outputFile.getAbsolutePath() + ".");

            os = new FileOutputStream(outputFile);
            byte[] buf = new byte[1024];
            int len;
            while ((len = is.read(buf)) > 0) {
                os.write(buf, 0, len);
            }

        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (Exception e) {
                    // nothing
                }
            }

            if (is != null)
                is.close();
        }
    }
}

Related

  1. copyFile(File in, File out)
  2. copyFile(File in, File out)
  3. copyFile(File inFile, File outFile)
  4. copyFile(File inFile, File outFile)
  5. copyFile(File input, File output)
  6. copyFile(File inputFile, File outputFile)
  7. copyFile(File inputFile, File outputFile)
  8. copyFile(File inputFile, OutputStream os)
  9. copyFile(File of, File nf)