Showing posts with label java file copying. Show all posts
Showing posts with label java file copying. Show all posts

Friday, May 1, 2009

Java file copying

using java.nio for super-speedy copies. More readable and less prone to errors than using Input/Output stream pairs and a byte buffer

try {
// Create channel on the source
FileChannel srcChannel = new FileInputStream("srcFilename").getChannel();
// Create channel on the destination
FileChannel dstChannel = newFileOutputStream("dstFilename").getChannel();
// Copy file contents from source to destination
dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
// Close the channels
srcChannel.close();
dstChannel.close();
} catch (IOException e) {
}