Java BufferedInputStream Copy copyFile(File fSrcFile, File fDestFile)

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

Description

Copies the file contents from one file to another.

License

Apache License

Parameter

Parameter Description
fSrcFile Source file
fDestFile Destination file. If this is a folder, file name is taken from the source file.

Exception

Parameter Description
IOExceptionThrown if the operation was not successful

Declaration

public static void copyFile(File fSrcFile, File fDestFile) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;

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 the file contents from one file to another. The destination file is overwritten.
     *
     * @param   fSrcFile   Source file
     * @param   fDestFile  Destination file. If this is a folder, file name is taken from the source
     *                     file.
     *
     * @throws  IOException  Thrown if the operation was not successful
     */
    public static void copyFile(File fSrcFile, File fDestFile) throws IOException {
        if (fDestFile.isDirectory()) {
            fDestFile = new File(fDestFile, fSrcFile.getName());
        }

        File fParent = fDestFile.getParentFile();

        if (!fParent.exists()) {
            if (!fParent.mkdirs()) {
                throw new IOException("Unable to create parent folder: " + fParent);
            }
        }

        InputStream in = null;
        OutputStream out = null;
        final int iCopyBufferSize = 32768;
        boolean bSuccess = false;

        try {
            // Open the source file
            in = new BufferedInputStream(new FileInputStream(fSrcFile));

            // Create the destination file
            out = new BufferedOutputStream(new FileOutputStream(fDestFile));

            // Create the buffer that is used while copying the data
            byte[] baBuffer = new byte[iCopyBufferSize];
            int iReadCount;

            // Copy the file contents
            while ((iReadCount = in.read(baBuffer)) > 0) {
                out.write(baBuffer, 0, iReadCount);
            }

            bSuccess = true;
            baBuffer = null;
        } finally {
            // Close the input stream
            closeStream(in);

            // Close the output stream
            closeStream(out);

            // If the copy operation failed, delete the destination file.
            if (!bSuccess && fDestFile.exists()) {
                fDestFile.delete();
            }
        }
    }

    /**
     * Closes an input stream. This ignores all exceptions that might be thrown.
     *
     * @param  isInput  The input stream to be closed. If it is null, this method does nothing.
     */
    public static void closeStream(InputStream isInput) {
        try {
            if (isInput != null) {
                isInput.close();
            }
        } catch (IOException ignored) {
        }
    }

    /**
     * Closes an output stream. This ignores all exceptions that might be thrown.
     *
     * @param  osOutput  The output stream to be closed. If it is null, this method does nothing.
     */
    public static void closeStream(OutputStream osOutput) {
        try {
            if (osOutput != null) {
                osOutput.close();
            }
        } catch (IOException ignored) {
        }
    }
}

Related

  1. copyFile(File from, File to)
  2. copyFile(File from, File to)
  3. copyFile(File from, File to)
  4. copyFile(File from, File to)
  5. copyFile(File fromFile, File toFile)
  6. copyFile(File in, File out)
  7. copyFile(File in, File out)
  8. copyFile(File inFile, File outFile)
  9. copyFile(File inFile, File outFile)