Here you can find the source of copyFile(File sourceFile, File destinationFile)
Parameter | Description |
---|---|
sourceFile | the source file |
destinationFile | the destination file |
Parameter | Description |
---|---|
IOException | Signals that an I/O exception has occurred. |
public static void copyFile(File sourceFile, File destinationFile) throws IOException
//package com.java2s; 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 v a 2s .c om*/ * Copy file. * * @param sourceFile the source file * @param destinationFile the destination file * @throws IOException Signals that an I/O exception has occurred. */ public static void copyFile(File sourceFile, File destinationFile) throws IOException { FileInputStream sourceIs = null; FileChannel source = null; FileOutputStream destinationOs = null; FileChannel destination = null; try { sourceIs = new FileInputStream(sourceFile); source = sourceIs.getChannel(); destinationOs = new FileOutputStream(destinationFile); destination = destinationOs.getChannel(); destination.transferFrom(source, 0, source.size()); } finally { if (source != null) { source.close(); } if (sourceIs != null) { sourceIs.close(); } if (destination != null) { destination.close(); } if (destinationOs != null) { destinationOs.close(); } } } }