Java FileInputStream Copy copyFile(final File from, final File to)

Here you can find the source of copyFile(final File from, final File to)

Description

Copies a file to a different location.

License

Open Source License

Parameter

Parameter Description
from Copy source.
to Copy destination.

Exception

Parameter Description
IOException File exception occurred.

Declaration

public static void copyFile(final File from, final File to) throws IOException 

Method Source Code

//package com.java2s;
/* This file is part of the project "Hilbert II" - http://www.qedeq.org
 *
 * Copyright 2000-2013,  Michael Meyling <mime@qedeq.org>.
 *
 * "Hilbert II" is free software; you can redistribute
 * it and/or modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *///w  w  w  . j a v  a  2 s . c  o m

import java.io.File;

import java.io.FileInputStream;
import java.io.FileOutputStream;

import java.io.IOException;
import java.io.InputStream;

import java.io.OutputStream;

import java.io.Reader;

import java.io.Writer;

public class Main {
    /**
     * Copies a file to a different location.
     *
     * @param   from    Copy source.
     * @param   to      Copy destination.
     * @throws  IOException File exception occurred.
     */
    public static void copyFile(final File from, final File to) throws IOException {

        if (from.getCanonicalFile().equals(to.getCanonicalFile())) {
            return;
        }
        createNecessaryDirectories(to);
        FileInputStream in = null;
        FileOutputStream out = null;
        try {
            in = new FileInputStream(from);
            out = new FileOutputStream(to);

            byte[] data = new byte[8 * 1024];
            int length;
            while ((length = in.read(data)) != -1) {
                out.write(data, 0, length);
            }
        } finally {
            close(in);
            close(out);
        }
    }

    /**
     * Creates necessary parent directories for a file.
     *
     * @param   file    File.
     * @throws  IOException Creation failed.
     */
    public static void createNecessaryDirectories(final File file) throws IOException {
        if (file != null && file.getParentFile() != null) {
            file.getParentFile().mkdirs();
            if (!file.getParentFile().exists()) {
                throw new IOException("directory creation failed: " + file.getParent());
            }
        }
    }

    /**
     * Closes input stream without exception.
     *
     * @param   in  Input stream, maybe <code>null</code>.
     */
    public static void close(final InputStream in) {
        if (in != null) {
            try {
                in.close();
            } catch (Exception e) {
                // ignore
            }
        }
    }

    /**
     * Closes writer without exception.
     *
     * @param   writer  Writer, maybe <code>null</code>.
     */
    public static void close(final Writer writer) {
        if (writer != null) {
            try {
                writer.close();
            } catch (Exception e) {
                // ignore
            }
        }
    }

    /**
     * Closes out stream without exception.
     *
     * @param   out Output stream, maybe <code>null</code>.
     */
    public static void close(final OutputStream out) {
        if (out != null) {
            try {
                out.close();
            } catch (Exception e) {
                // ignore
            }
        }
    }

    /**
     * Closes input reader without exception.
     *
     * @param   reader  Reader, maybe <code>null</code>.
     */
    public static void close(final Reader reader) {
        if (reader != null) {
            try {
                reader.close();
            } catch (Exception e) {
                // ignore
            }
        }
    }
}

Related

  1. copyFile(File srcPath, File dstPath)
  2. copyfile(File srFile, File dtFile)
  3. copyFile(File target, File source)
  4. copyFile(File targetFile, File file)
  5. copyFile(File targetFile, File file)
  6. copyFile(final File fromFile, final File toFile)
  7. copyFile(final File fromFile, final File toFile)
  8. copyFile(final File in, final File out)
  9. copyFile(final File oldfile, final File newfile)