Java FileInputStream Copy copyFile(File src, File dst)

Here you can find the source of copyFile(File src, File dst)

Description

copy File

License

Open Source License

Declaration

public static void copyFile(File src, File dst) throws IOException 

Method Source Code

//package com.java2s;
/**/*w  w w.  j av a  2  s .  c  o m*/
 * File: $HeadURL: https://hdt-java.googlecode.com/svn/trunk/hdt-java/src/org/rdfhdt/hdt/util/io/IOUtil.java $
 * Revision: $Rev: 194 $
 * Last modified: $Date: 2013-03-04 21:30:01 +0000 (lun, 04 mar 2013) $
 * Last modified by: $Author: mario.arias $
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Contacting the authors:
 *   Mario Arias:               mario.arias@deri.org
 *   Javier D. Fernandez:       jfergar@infor.uva.es
 *   Miguel A. Martinez-Prieto: migumar2@infor.uva.es
 *   Alejandro Andres:          fuzzy.alej@gmail.com
 */

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(File src, File dst) throws IOException {
        FileInputStream in = new FileInputStream(src);
        FileOutputStream out = new FileOutputStream(dst);
        try {
            copyStream(in, out);
        } finally {
            closeQuietly(in);
            closeQuietly(out);
        }
    }

    public static void copyStream(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[1024 * 1024];
        int len;
        while ((len = in.read(buffer)) != -1) {
            out.write(buffer, 0, len);
        }
    }

    public static void copyStream(InputStream in, OutputStream out, long n) throws IOException {
        byte[] buffer = new byte[1024 * 1024];
        int len = (int) (buffer.length < n ? buffer.length : n);
        long total = 0;

        while ((total < n) && (len = in.read(buffer, 0, len)) != -1) {
            out.write(buffer, 0, len);

            total += len;
            len = (int) (total + buffer.length > n ? n - total : buffer.length);
        }
    }

    public static void closeQuietly(InputStream input) {
        if (input == null)
            return;

        try {
            input.close();
        } catch (IOException e) {
        }
    }

    public static void closeQuietly(OutputStream output) {
        if (output == null)
            return;

        try {
            output.close();
        } catch (IOException e) {
        }
    }
}

Related

  1. copyFile(File src, File dst)
  2. copyFile(File src, File dst)
  3. copyFile(File src, File dst)
  4. copyFile(File src, File dst)
  5. copyFile(File src, File dst)
  6. copyFile(File src, File dst, boolean copyLMD)
  7. copyFile(File src, File target)
  8. copyFile(File src, File toDir)
  9. copyFile(File srcFile, File destFile)