Here you can find the source of copyFile(File source, File dest, boolean deleteIfExists)
Parameter | Description |
---|---|
source | File Source file |
dest | File Destination file |
deleteIfExists | boolean Determines whether the copy goes on even if the file exists. |
Parameter | Description |
---|---|
IOException | an exception |
public static boolean copyFile(File source, File dest, boolean deleteIfExists) throws IOException
//package com.java2s; /* Copyright (C) 2003-2015 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./*from ww w. j a va2 s . c om*/ 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.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; 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. * @return boolean Whether the copy succeeded, or was stopped due to the * file already existing. * @throws IOException */ public static boolean copyFile(File source, File dest, boolean deleteIfExists) throws IOException { // Check if the file already exists. if (dest.exists() && !deleteIfExists) { return false; } try (BufferedInputStream in = new BufferedInputStream(new FileInputStream(source)); BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(dest))) { int el; while ((el = in.read()) >= 0) { out.write(el); } out.flush(); } return true; } }