Standard concise way to copy a file in Java?

It has always bothered me that the only way to copy a file in Java involves opening streams, declaring a buffer, reading in one file, looping through it, and writing it out to the other steam. The web is littered with similar, yet still slightly different implementations of this type of solution.

Is there a better way that stays within the bounds of the Java language (meaning does not involve exec-ing OS specific commands)? Perhaps in some reliable open source utility package, that would at least obscure this underlying implementation and provide a one line solution?

As toolkit mentions above, Apache Commons IO is the way to go, specifically FileUtils.copyFile(); it handles all the heavy lifting for you.


And as a postscript, note that recent versions of FileUtils (such as the 2.0.1 release) have added the use of NIO for copying files; NIO can significantly increase file-copying performance, in a large part because the NIO routines defer copying directly to the OS/filesystem rather than handle it by reading and writing bytes through the Java layer. So if you're looking for performance, it might be worth checking that you are using a recent version of FileUtils.

for copying file in java we have to use the fileInputStream() and fileOutputStream() methods

example program shown in bellow:public static void copyFile(File sourceFile, File destFile) throws IOException {

    if(!destFile.exists()) {




    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) {



        if(destination != null) {