Java BufferedInputStream Copy copyFile(File src, File dst)

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

Description

Copy one file to another.

License

BSD License

Parameter

Parameter Description
src The file to copy
dst Where to copy it. Can be a directory or a file.

Exception

Parameter Description
IOException on error

Return

total number of bytes copied

Declaration

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

Method Source Code


//package com.java2s;
/*---------------------------------------------------------------------------*\
  $Id: ccaeaa89461ad4ae737ab4fc7568675f1a9bfa36 $
  ---------------------------------------------------------------------------
  This software is released under a BSD-style license:
    /*from  w ww. j  a v  a  2  s  . c om*/
  Copyright (c) 2004-2007 Brian M. Clapper. All rights reserved.
    
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are
  met:
    
  1.  Redistributions of source code must retain the above copyright notice,
  this list of conditions and the following disclaimer.
    
  2.  The end-user documentation included with the redistribution, if any,
  must include the following acknowlegement:
    
"This product includes software developed by Brian M. Clapper
(bmc@clapper.org, http://www.clapper.org/bmc/). That software is
copyright (c) 2004-2007 Brian M. Clapper."
    
  Alternately, this acknowlegement may appear in the software itself,
  if wherever such third-party acknowlegements normally appear.
    
  3.  Neither the names "clapper.org", "clapper.org Java Utility Library",
  nor any of the names of the project contributors may be used to
  endorse or promote products derived from this software without prior
  written permission. For written permission, please contact
  bmc@clapper.org.
    
  4.  Products derived from this software may not be called "clapper.org
  Java Utility Library", nor may "clapper.org" appear in their names
  without prior written permission of Brian M. Clapper.
    
  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  NO EVENT SHALL BRIAN M. CLAPPER BE LIABLE FOR ANY DIRECT, INDIRECT,
  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\*---------------------------------------------------------------------------*/

import java.io.*;

public class Main {
    /**
     * Copy one file to another. This method simply copies bytes and
     * performs no character set conversion. If you want character set
     * conversions, use {@link #copyTextFile(File,String,File,String)}.
     *
     * @param src  The file to copy
     * @param dst  Where to copy it. Can be a directory or a file.
     *
     * @return total number of bytes copied
     *
     * @throws IOException on error
     *
     * @see #copyTextFile(File,String,File,String)
     * @see #copyReader(Reader,Writer,int)
     * @see #copyReader(Reader,Writer)
     * @see #copyStream(InputStream,OutputStream,int)
     * @see #copyStream(InputStream,OutputStream)
     */
    public static int copyFile(File src, File dst) throws IOException {
        int totalCopied = 0;

        if (dst.isDirectory())
            dst = new File(dst, src.getName());

        InputStream from = null;
        OutputStream to = null;

        try {
            from = new FileInputStream(src);
            to = new FileOutputStream(dst);

            totalCopied = copyStream(from, to);
        }

        finally {
            if (from != null)
                from.close();

            if (to != null)
                to.close();
        }

        return totalCopied;
    }

    /**
     * Copy an <tt>InputStream</tt> to an <tt>OutputStream</tt>. If either
     * stream is not already buffered, then it's wrapped in the corresponding
     * buffered stream (i.e., <tt>BufferedInputStream</tt> or
     * <tt>BufferedOutputStream</tt>) before copying. Calling this method
     * is equivalent to:
     *
     * <blockquote><pre>copyStream (src, dst, 8192);</pre></blockquote>
     *
     * @param is    the source <tt>InputStream</tt>
     * @param os    the destination <tt>OutputStream</tt>
     *
     * @return total number of bytes copied
     *
     * @throws IOException  on error
     *
     * @see #copyStream(InputStream,OutputStream,int)
     * @see #copyReader(Reader,Writer)
     * @see #copyFile(File,File)
     */
    public static int copyStream(InputStream is, OutputStream os) throws IOException {
        return copyStream(is, os, -1);
    }

    /**
     * Copy an <tt>InputStream</tt> to an <tt>OutputStream</tt>. If either
     * stream is not already buffered, then it's wrapped in the corresponding
     * buffered stream (i.e., <tt>BufferedInputStream</tt> or
     * <tt>BufferedOutputStream</tt>) before copying.
     *
     * @param src        the source <tt>InputStream</tt>
     * @param dst        the destination <tt>OutputStream</tt>
     * @param bufferSize the buffer size to use, or -1 for a default
     *
     * @return total number of bytes copied
     *
     * @throws IOException  on error
     *
     * @see #copyReader(Reader,Writer,int)
     * @see #copyStream(InputStream,OutputStream)
     * @see #copyFile(File,File)
     */
    public static int copyStream(InputStream src, OutputStream dst, int bufferSize) throws IOException {
        int totalCopied = 0;

        if (!(src instanceof BufferedInputStream)) {
            if (bufferSize > 0)
                src = new BufferedInputStream(src, bufferSize);
            else
                src = new BufferedInputStream(src);
        }

        if (!(dst instanceof BufferedOutputStream)) {
            if (bufferSize > 0)
                dst = new BufferedOutputStream(dst, bufferSize);
            else
                dst = new BufferedOutputStream(dst);
        }

        int b;

        while ((b = src.read()) != -1) {
            dst.write(b);
            totalCopied++;
        }

        dst.flush();

        return totalCopied;
    }
}

Related

  1. copyFile(File sourceFile, File targetFile)
  2. copyFile(File src, File dest)
  3. copyFile(File src, File dest)
  4. copyFile(File src, File dest)
  5. copyFile(File src, File dest)
  6. copyFile(File src, File dst)
  7. copyFile(File src, File dst)
  8. copyFile(File src, File target)
  9. copyFile(File src, File targetDir, boolean onlyNew)