Here you can find the source of copy(File sourceFile, File destFile)
Parameter | Description |
---|---|
sourceFile | the source |
destFile | the destination (will be created if doesn't exist, will be overwritten if it exists) |
Parameter | Description |
---|---|
IOException | we never know... |
public static void copy(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 { /**//from w ww. j a v a 2 s. c o m * Copy a source file to a specified destination path. * @param sourceFile the source * @param destFile the destination (will be created if doesn't exist, will * be overwritten if it exists) * @throws IOException we never know... */ public static void copy(File sourceFile, File destFile) throws IOException { if (!destFile.exists()) { destFile.createNewFile(); } FileChannel source = null; FileChannel destination = null; try { source = new FileInputStream(sourceFile).getChannel(); destination = new FileOutputStream(destFile).getChannel(); destination.transferFrom(source, 0, source.size()); } finally { if (source != null) { source.close(); } if (destination != null) { destination.close(); } } } }