There is no utility in the Java libraries to copy a file or recursivly copy a directory. There are philisophical reasons for this ranging from the wide selection of ways the process can fail down to performance whingers. At the end of the day though you need to be able to copy files and directories so here is a short utility class that will carry out those tasks. There are no fancy features in this class it just copies from source to destination. Extensions to this class might filter out certain files or provide moving rather than just copying.

package example;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

public class FileUtilities {

 
private FileUtilities() {}
 
 
public static final void copy( File source, File destination ) throws IOException {
   
if( source.isDirectory() ) {
     
copyDirectory( source, destination );
   
} else {
     
copyFile( source, destination );
   
}
  }
 
 
public static final void copyDirectory( File source, File destination ) throws IOException {
   
if( !source.isDirectory() ) {
     
throw new IllegalArgumentException( "Source (" + source.getPath() + ") must be a directory." );
   
}
   
   
if( !source.exists() ) {
     
throw new IllegalArgumentException( "Source directory (" + source.getPath() + ") doesn't exist." );
   
}
   
   
if( destination.exists() ) {
     
throw new IllegalArgumentException( "Destination (" + destination.getPath() + ") exists." );
   
}
   
   
destination.mkdirs();
    File
[] files = source.listFiles();
   
   
for( File file : files ) {
     
if( file.isDirectory() ) {
       
copyDirectory( file, new File( destination, file.getName() ) );
     
} else {
       
copyFile( file, new File( destination, file.getName() ) );
     
}
    }
  }
 
 
public static final void copyFile( File source, File destination ) throws IOException {
   
FileChannel sourceChannel = new FileInputStream( source ).getChannel();
    FileChannel targetChannel =
new FileOutputStream( destination ).getChannel();
    sourceChannel.transferTo
(0, sourceChannel.size(), targetChannel);
    sourceChannel.close
();
    targetChannel.close
();
 
}
}