Java FileInputStream Copy copyFile(final File source, final File target)

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

Description

Copy a file

License

Open Source License

Parameter

Parameter Description
source a parameter
target a parameter

Declaration

public static void copyFile(final File source, final File target) 

Method Source Code

//package com.java2s;
/**//from   ww  w.  ja va 2s. co  m
 * This file is part of Atomic Tagging.
 * 
 * Atomic Tagging 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 3 of the License, or (at your option) any later
 * version.
 * 
 * Atomic Tagging 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.
 * 
 * You should have received a copy of the GNU General Public License along with Atomic Tagging. If not, see
 * <http://www.gnu.org/licenses/>.
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class Main {
    /**
     * Copy a file
     * 
     * @param source
     * @param target
     */
    public static void copyFile(final File source, final File target) {
        if (!source.exists() || !source.canRead()) {
            throw new IllegalArgumentException("Can't read from given source file: " + source.getAbsolutePath());
        }
        if (!target.exists()) {
            try {
                if (!target.createNewFile()) {
                    throw new IllegalArgumentException(
                            "Can't write to given target file: " + target.getAbsolutePath());
                }
            } catch (final IOException e) {
                throw new IllegalArgumentException("Can't write to given target file: " + target.getAbsolutePath(),
                        e);
            }
        }

        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream(source);
            fos = new FileOutputStream(target);
        } catch (final FileNotFoundException ignore) {
            // Was checked previously.
        }

        if (fis == null || fos == null) {
            throw new RuntimeException("Failed to create file streams.");
        }

        try {
            final byte[] buf = new byte[1024];
            int i = 0;
            while ((i = fis.read(buf)) != -1) {
                fos.write(buf, 0, i);
            }
        } catch (final IOException e) {
            // FIXME
            throw new RuntimeException("Failed to copy file.", e);
        } finally {
            try {
                fis.close();
                fos.close();
            } catch (final IOException e) {
                // Nothing we can do, or is there?
                e.printStackTrace();
            }
        }
    }
}

Related

  1. copyFile(final File fromFile, final File toFile)
  2. copyFile(final File fromFile, final File toFile)
  3. copyFile(final File in, final File out)
  4. copyFile(final File oldfile, final File newfile)
  5. copyFile(final File source, final File dest)
  6. copyFile(final File src, final File dest)
  7. copyFile(final File src, final File dst)
  8. copyFile(final File srcFile, final File destFile)
  9. copyFile(final File srcPath, final File dstPath)