Here you can find the source of copyFile(final File fSource, final File fDest)
fSource
to the file fDest
.
Parameter | Description |
---|---|
fSource | the source file. |
fDest | the destination file. |
Parameter | Description |
---|---|
NullPointerException | if any of the method parameter is null. |
IOException | if an I/O error occurs while performing the copy. |
public static void copyFile(final File fSource, final File fDest) throws NullPointerException, IOException
//package com.java2s; /*//from w w w . j a va2 s .co m * MiscUtils.java * * Created: 23 avr. 09 * * Copyright (C) 2009 Julien Aubin * * 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. * * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class Main { /** * Copy the file <code>fSource</code> to the file <code>fDest</code>. Note that * if the directory which contains <code>fDest</code> does not exist this method * will attempt to create it. * @param fSource the source file. * @param fDest the destination file. * @throws NullPointerException if any of the method parameter is null. * @throws IOException if an I/O error occurs while performing the copy. */ public static void copyFile(final File fSource, final File fDest) throws NullPointerException, IOException { if (fSource == null || fDest == null) throw new NullPointerException(); File fDestDir = fDest.getParentFile(); if (fDestDir != null) { if (fDestDir.exists()) { if (fDestDir.isFile()) throw new FileNotFoundException( "The destination directory " + fDestDir + " is not a directory !"); } else if (!fDestDir.mkdirs()) { throw new FileNotFoundException("Unable to create the destination directory " + fDestDir); } } // We perform the copy by blocks of 32 MBs final int BLOCK_SIZE = 32 * 1024 * 1024; InputStream is = null; OutputStream os = null; try { long length = fSource.length(); is = new BufferedInputStream(new FileInputStream(fSource)); os = new BufferedOutputStream(new FileOutputStream(fDest)); long nbIter = length / BLOCK_SIZE; byte[] data = new byte[BLOCK_SIZE]; // In case the file is bigger than 32 MB... for (long i = 0; i < nbIter; i++) { is.read(data); os.write(data); } int remainingData = (int) (length % BLOCK_SIZE); is.read(data, 0, remainingData); os.write(data, 0, remainingData); } finally { try { if (is != null) { is.close(); } } finally { if (os != null) { os.close(); } } } } }