Java FileInputStream Copy copyFile(File sourceFile, File destFile)

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

Description

copy File

License

Open Source License

Declaration

public static void copyFile(File sourceFile, File destFile) 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;

public class Main {
    public static void copyFile(File sourceFile, File destFile) throws IOException {
        copyFile(sourceFile, destFile, false);
    }/* w  ww  . jav  a2s . c o m*/

    public static void copyFile(File sourceFile, File destFile, final boolean overwrite) throws IOException {
        InputStream sourceInput = null;
        try {
            sourceInput = new FileInputStream(sourceFile);
            copyFile(sourceInput, destFile, overwrite);
        } finally {
            if (sourceInput != null) {
                sourceInput.close();
            }
        }

    }

    public static void copyFile(InputStream sourceInput, File destFile, final boolean overwrite)
            throws IOException {
        if (destFile.exists()) {
            if (overwrite)
                destFile.delete();
            else
                return;
        } else {
            destFile.createNewFile();
        }

        FileOutputStream destOutput = null;
        try {
            destOutput = new FileOutputStream(destFile);
            int readBytes = 0;
            byte[] buffer = new byte[4096];
            while ((readBytes = sourceInput.read(buffer)) > 0) {
                destOutput.write(buffer, 0, readBytes);
            }
        } finally {
            if (destOutput != null) {
                destOutput.close();
            }
        }
    }
}

Related

  1. copyFile(File source, File target, boolean replaceIfExists)
  2. copyFile(File sourceDir, File destDir, String filename)
  3. copyFile(File sourceFile, File destDir)
  4. copyFile(File sourceFile, File destFile)
  5. copyFile(File sourceFile, File destFile)
  6. copyFile(File sourceFile, File destFile)
  7. copyFile(File sourceFile, File destFile)
  8. copyFile(File sourceFile, File destFile)
  9. copyFile(File sourceFile, File destFile, boolean overwrite, boolean preserveLastModified)