Java FileInputStream Copy copyFile(File pSrc, File pDst, boolean pForce)

Here you can find the source of copyFile(File pSrc, File pDst, boolean pForce)

Description

Copy the file src into the file dst.

License

LGPL

Parameter

Parameter Description
pSrc the original file
pDst the file to create
pForce overwrite the existing destination if any

Exception

Parameter Description
IOException is thrown if <ul> <li>the src file is <code>null</code> orisn't readable,</li> <li>dst is <code>null</code></li><li>the writing process fails</li> </ul>

Declaration

public static void copyFile(File pSrc, File pDst, boolean pForce) throws IOException 

Method Source Code

//package com.java2s;
/*************************************************************************
 *
 * $RCSfile: FileHelper.java,v $//w  w  w.  jav  a  2 s.  c o m
 *
 * $Revision: 1.3 $
 *
 * last change: $Author: cedricbosdo $ $Date: 2007/11/25 20:32:31 $
 *
 * The Contents of this file are made available subject to the terms of
 * the GNU Lesser General Public License Version 2.1
 *
 * Sun Microsystems Inc., October, 2000
 *
 *
 * GNU Lesser General Public License Version 2.1
 * =============================================
 * Copyright 2000 by Sun Microsystems, Inc.
 * 901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software Foundation.
 *
 * 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., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 * 
 * The Initial Developer of the Original Code is: Sun Microsystems, Inc..
 *
 * Copyright: 2002 by Sun Microsystems, Inc.
 *
 * All Rights Reserved.
 *
 * Contributor(s): Cedric Bosdonnat
 *
 *
 ************************************************************************/

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

public class Main {
    /**
     * Copy the file src into the file dst. If the dst file already exists, it
     * will be deleted before to start copying if force is set to
     * <code>true</code>, otherwise nothing will be done.
     * 
     * @param pSrc
     *            the original file
     * @param pDst
     *            the file to create
     * @param pForce
     *            overwrite the existing destination if any
     * 
     * @throws IOException
     *             is thrown if <ul> <li>the src file is <code>null</code> or
     *             isn't readable,</li> <li>dst is <code>null</code></li>
     *             <li>the writing process fails</li> </ul>
     */
    public static void copyFile(File pSrc, File pDst, boolean pForce) throws IOException {

        // Check for invalid arguments
        if (pSrc == null || !pSrc.canRead()) {
            throw new IOException("FileHelper.ReadError" + (pSrc == null ? "" : pSrc.getAbsolutePath()));
        }

        if (pDst == null) {
            throw new IOException("FileHelper.NullDestinationError");
        }

        // clean the existing file if any and force
        if (pForce && pDst.exists() && pDst.isFile()) {
            pDst.delete();
        }

        if (!pDst.exists()) {
            // now copy the file
            FileInputStream in = new FileInputStream(pSrc);
            FileOutputStream out = new FileOutputStream(pDst);

            try {
                int c = in.read();
                while (c != -1) {
                    out.write(c);
                    c = in.read();
                }
            } finally {
                try {
                    in.close();
                    out.close();
                } catch (Exception e) {
                }
            }
        }
    }
}

Related

  1. copyFile(File orig, File dest, boolean overwrite)
  2. copyFile(File original, File copy)
  3. copyFile(File original, File destination)
  4. copyFile(File original, File parent)
  5. copyFile(File originalFile, File destinationDir)
  6. copyFile(File source, File dest)
  7. copyFile(File source, File dest)
  8. copyFile(File source, File dest)
  9. copyFile(File source, File dest)