Java Snippet – Fast File Copy

When using JDK 1.4 and above…
[java]
public static void copyFile(File in, File out) {
try {
FileChannel sourceChannel = new FileInputStream(in).getChannel();
FileChannel destinationChannel = new FileOutputStream(out).getChannel();
sourceChannel.transferTo(0, sourceChannel.size(), destinationChannel);
// or, you can also copy it this way
// destinationChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
sourceChannel.close();
destinationChannel.close();
} catch ( Exception e ) {
e.printStackTrace();
}
}
[/java]

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top