Java FileInputStream Copy copyFile(File srcFile, File destFile)

Here you can find the source of copyFile(File srcFile, File destFile)

Description

Copy a file to another

License

Open Source License

Parameter

Parameter Description
srcFile The file to copy
destFile The file where srcFile is copied

Declaration

public static void copyFile(File srcFile, File destFile) throws IOException 

Method Source Code


//package com.java2s;
/* /*from   w w w  . j  av  a 2s  .  c  o m*/
 * Copyright (C) 2016 Bruce Beisel
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */

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

public class Main {
    /**
     * Copy a file to another
     * 
     * @param srcFile The file to copy
     * @param destFile The file where srcFile is copied
     * 
     * @exception IOException Thrown if either the source cannot be read or the destination cannot be written
     */
    public static void copyFile(File srcFile, File destFile) throws IOException {
        FileOutputStream os = null;
        try (FileInputStream is = new FileInputStream(srcFile)) {
            os = new FileOutputStream(destFile);
            byte[] buffer = new byte[1024];
            int bcount;
            while ((bcount = is.read(buffer)) > 0)
                os.write(buffer, 0, bcount);
        } finally {
            if (os != null)
                os.close();
        }
    }
}

Related

  1. copyFile(File src, File dst)
  2. copyFile(File src, File dst)
  3. copyFile(File src, File dst, boolean copyLMD)
  4. copyFile(File src, File target)
  5. copyFile(File src, File toDir)
  6. copyFile(File srcFile, File destFile)
  7. copyFile(File srcFile, File destFile)
  8. copyFile(File srcFile, File destFile)
  9. copyFile(File srcFile, File destFile)