Here you can find the source of copyFile(File sourceFile, File destFile)
Parameter | Description |
---|---|
sourceFile | the source file |
destFile | the destination file |
Parameter | Description |
---|---|
IOException | Signals that an I/O exception has occurred. |
public static void copyFile(File sourceFile, File destFile) throws IOException
//package com.java2s; //License from project: Open Source License import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.channels.FileChannel; public class Main { /**/* ww w . j a va 2s . c o m*/ * Copy file from a path to another. * * @param sourceFile the source file * @param destFile the destination file * @throws IOException Signals that an I/O exception has occurred. */ public static void copyFile(File sourceFile, File destFile) throws IOException { if (!destFile.exists()) { destFile.createNewFile(); } FileInputStream fin = new FileInputStream(sourceFile); try { FileOutputStream fout = new FileOutputStream(destFile); try { FileChannel source = fin.getChannel(); try { FileChannel destination = fout.getChannel(); try { long count = 0; long size = source.size(); while ((count += destination.transferFrom(source, count, size - count)) < size) ; } finally { destination.close(); } } finally { source.close(); } } finally { fout.close(); } } finally { fin.close(); } } }