Java Snippet - Fast File Copy

When using JDK 1.4 and above...

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();
    }
}


About this entry