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

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

Description

copy File

License

Open Source License

Declaration

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

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2011 Red Hat, Inc./*from  www.jav a 2s. c  o m*/
 * Distributed under license by Red Hat, Inc. All rights reserved.
 * This program is 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:
 *     Red Hat, Inc. - initial API and implementation
 ******************************************************************************/

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import org.eclipse.core.runtime.Assert;

public class Main {
    private static final byte[] buffer = new byte[1024];

    private static void copyFile(File source, File destination, boolean overwrite) throws IOException {
        Assert.isLegal(source != null);
        Assert.isLegal(source.isFile());
        Assert.isLegal(destination != null);

        destination = getDestinationFile(source, destination);

        if (exists(destination) && !overwrite) {
            return;
        }

        if (isDirectory(destination)) {
            if (!overwrite) {
                return;
            }
            destination.delete();
        }

        writeTo(source, destination);
    }

    private static File getDestinationFile(File source, File destination) {
        if (!source.getName().equals(destination.getName())) {
            destination = new File(destination, source.getName());
        }
        return destination;
    }

    public static boolean exists(File file) {
        return file != null && file.exists();
    }

    public static boolean isDirectory(File file) {
        return file != null && file.isDirectory();
    }

    private static final void writeTo(File source, File destination) throws IOException {
        Assert.isLegal(source != null);
        Assert.isLegal(destination != null);

        writeTo(new BufferedInputStream(new FileInputStream(source)), destination);
    }

    public static final void writeTo(String content, File destination) throws FileNotFoundException {
        PrintWriter writer = new PrintWriter(destination);
        writer.write(content);
        writer.flush();
        writer.close();
    }

    private static final void writeTo(InputStream in, File destination) throws IOException {
        Assert.isLegal(in != null);
        Assert.isLegal(destination != null);

        OutputStream out = null;
        try {
            out = new BufferedOutputStream(new FileOutputStream(destination));
            for (int read = -1; (read = in.read(buffer)) != -1;) {
                out.write(buffer, 0, read);
            }
            out.flush();
        } finally {
            silentlyClose(in);
            silentlyClose(out);
        }
    }

    private static void silentlyClose(InputStream in) {
        try {
            if (in != null) {
                in.close();
            }
        } catch (IOException e) {
            // ignore
        }
    }

    private static void silentlyClose(OutputStream out) {
        try {
            if (out != null) {
                out.close();
            }
        } catch (IOException e) {
            // ignore
        }
    }
}

Related

  1. copyFile(File source, File dest)
  2. copyFile(File source, File dest, boolean deleteIfExists)
  3. copyFile(File source, File dest, boolean deleteIfExists)
  4. copyFile(File source, File destination)
  5. copyFile(File source, File destination)
  6. copyFile(File source, File target)
  7. copyFile(File source, File target)
  8. copyFile(File source, File target)
  9. copyFile(File sourceFile, File targetFile)