Java FileInputStream Copy copyFile(File source, File dest)

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

Description

copy File

License

Open Source License

Declaration

public static void copyFile(File source, File dest) throws IOException 

Method Source Code


//package com.java2s;
/*//  w  w  w .  j  a v  a2 s.  co m
 * Copyright (c) 2012 EDC
 * 
 * This file is part of Stepping Stone.
 * 
 * Stepping Stone 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.
 * 
 * Stepping Stone 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 Stepping Stone.  If not, see <http://www.gnu.org/licenses/gpl.txt>.
 */

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.util.zip.ZipFile;

public class Main {
    public static void copyFile(File source, File dest) throws IOException {
        copyStream(new FileInputStream(source), new FileOutputStream(dest));
    }

    /**
     * Copy all bytes from in to out. Flush out at the end but do not close the streams.
     * 
     * @param in
     * @param out
     * @throws IOException
     */
    public static int copyStream(InputStream in, OutputStream out, boolean closeIn, boolean closeOut)
            throws IOException {
        int totalBytes = 0;
        try {
            byte[] buff = new byte[1024 * 4];
            int bytesRead = 0;
            while ((bytesRead = in.read(buff)) != -1) {
                if (bytesRead > 0) {
                    out.write(buff, 0, bytesRead);
                    totalBytes += bytesRead;
                }
            }
            out.flush();
        } finally {
            if (closeIn)
                closeGracefully(in);

            if (closeOut)
                closeGracefully(out);
        }
        return totalBytes;
    }

    public static int copyStream(InputStream in, OutputStream out) throws IOException {
        return copyStream(in, out, true, true);
    }

    public static void closeGracefully(InputStream is) {
        if (is != null) {
            try {
                is.close();
            } catch (IOException ignore) {
            }
        }
    }

    public static void closeGracefully(Reader r) {
        if (r != null) {
            try {
                r.close();
            } catch (IOException ignore) {
            }
        }
    }

    public static void closeGracefully(OutputStream out) {
        if (out != null) {
            try {
                out.close();
            } catch (IOException ignore) {
            }
        }
    }

    public static void closeGracefully(ZipFile zipFile) {
        if (zipFile != null) {
            try {
                zipFile.close();
            } catch (IOException ignore) {
            }
        }
    }
}

Related

  1. copyFile(File pSrc, File pDst, boolean pForce)
  2. copyFile(File source, File dest)
  3. copyFile(File source, File dest)
  4. copyFile(File source, File dest)
  5. copyFile(File source, File dest)
  6. copyFile(File source, File dest)
  7. copyFile(File source, File dest)
  8. copyFile(File source, File dest)
  9. copyFile(File source, File destination)