Java BufferedInputStream Copy copyFile(File source, File dest, boolean deleteIfExists)

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

Description

Copies a file.

License

Open Source License

Parameter

Parameter Description
source File Source file
dest File Destination file
deleteIfExists boolean Determines whether the copy goes on even if the file exists.

Exception

Parameter Description
IOException an exception

Return

boolean Whether the copy succeeded, or was stopped due to the file already existing.

Declaration

public static boolean copyFile(File source, File dest, boolean deleteIfExists) throws IOException 

Method Source Code

//package com.java2s;
/*  Copyright (C) 2003-2012 JabRef contributors.
This program 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 2 of the License, or
(at your option) any later version.// www  .j  av  a2  s .co  m
    
This program 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 this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

import java.io.*;

public class Main {
    /**
     * Copies a file.
     * 
     * @param source
     *            File Source file
     * @param dest
     *            File Destination file
     * @param deleteIfExists
     *            boolean Determines whether the copy goes on even if the file
     *            exists.
     * @throws IOException
     * @return boolean Whether the copy succeeded, or was stopped due to the
     *         file already existing.
     */
    public static boolean copyFile(File source, File dest, boolean deleteIfExists) throws IOException {

        BufferedInputStream in = null;
        BufferedOutputStream out = null;
        try {
            // Check if the file already exists.
            if (dest.exists()) {
                if (!deleteIfExists)
                    return false;
                // else dest.delete();
            }

            in = new BufferedInputStream(new FileInputStream(source));
            out = new BufferedOutputStream(new FileOutputStream(dest));
            int el;
            // int tell = 0;
            while ((el = in.read()) >= 0) {
                out.write(el);
            }
        } finally {
            if (out != null) {
                out.flush();
                out.close();
            }
            if (in != null) {
                in.close();
            }
        }
        return true;
    }
}

Related

  1. copyFile(File inFile, File outFile)
  2. copyFile(File inFile, File outFile)
  3. copyFile(File inputFile, File outputFile)
  4. copyFile(File origem, File destino)
  5. copyFile(File source, File dest)
  6. copyFile(File source, File dest, boolean deleteIfExists)
  7. copyFile(File source, File destination)
  8. copyFile(File source, File destination)
  9. copyFile(File source, File destination, boolean overwrite)