Java BufferedInputStream Copy copyFile(final File source, final File destination, final boolean overwrite)

Here you can find the source of copyFile(final File source, final File destination, final boolean overwrite)

Description

copy File

License

Open Source License

Declaration

public static void copyFile(final File source, final File destination, final boolean overwrite)
            throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source 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 {
    public static void copyFile(final File source, final File destination, final boolean overwrite)
            throws IOException {
        if (source == null) {
            throw new NullPointerException("source");
        }/*  ww  w  .j a  v  a2 s.c o m*/
        if (!source.exists()) {
            throw new IllegalArgumentException("source");
        }
        if (!source.canRead()) {
            throw new IllegalArgumentException("source");
        }
        if (!source.isFile()) {
            throw new IllegalArgumentException("source");
        }
        if (destination == null) {
            throw new NullPointerException("destination");
        }
        if (destination.exists() && !destination.isFile()) {
            throw new IllegalArgumentException("destination");
        }
        if (destination.exists() && !destination.canWrite()) {
            throw new IllegalArgumentException("destination");
        }

        File destinationParent = destination.getParentFile();
        destinationParent.mkdirs();
        if (!destinationParent.exists()) {
            throw new IllegalArgumentException("destinationParent");
        }
        if (!destinationParent.canWrite()) {
            throw new IllegalArgumentException("destinationParent");
        }
        if (!destinationParent.isDirectory()) {
            throw new IllegalArgumentException("destinationParent");
        }
        if (destination.exists() && !overwrite) {
            return;
        }

        InputStream is = null;
        BufferedInputStream bis = null;
        OutputStream os = null;
        BufferedOutputStream bos = null;
        try {
            is = new FileInputStream(source);
            bis = new BufferedInputStream(is);
            os = new FileOutputStream(destination);
            bos = new BufferedOutputStream(os);

            byte[] buffer = new byte[2048];
            int length = 0;
            while ((length = bis.read(buffer)) > 0) {
                bos.write(buffer, 0, length);
            }
        } finally {
            try {
                try {
                    if (is != null) {
                        is.close();
                    }
                } finally {
                    if (bis != null) {
                        bis.close();
                    }
                }
            } finally {
                try {
                    if (bos != null) {
                        bos.close();
                    }
                } finally {
                    if (os != null) {
                        os.close();
                    }
                }
            }
        }
    }
}

Related

  1. copyFile(File srcFile, File targetFolder)
  2. copyFile(final File from, final File to)
  3. copyFile(final File fSource, final File fDest)
  4. copyFile(final File source, final File dest, final boolean overwrite)
  5. copyFile(final File source, final File destination)
  6. copyFile(final File sourceFile, final File targetFile)
  7. copyFile(final File src, final File dest)
  8. copyFile(final File to, final File from)
  9. copyFile(InputStream is, File newFile)