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-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; } }