Skip to content

Instantly share code, notes, and snippets.

@marcelmaatkamp
Created March 29, 2010 11:49
Show Gist options
  • Save marcelmaatkamp/347776 to your computer and use it in GitHub Desktop.
Save marcelmaatkamp/347776 to your computer and use it in GitHub Desktop.
import java.nio.*
/**
* Fast copy with java.nio.channels
*
* Usage:
*
* FileUtils.fastCopy(new File("from.bin"), new File("to.bin"))
*
* @author Marcel Maatkamp (m.maatkamp avec gmail dot com)
*/
class FileUtils {
static fastCopy(from, to) {
def ic=null, oc=null
assert from.canRead()
assert to.canWrite()
try {
if(!to.exists()) {
to.createNewFile()
}
ic = new FileInputStream(from).channel
oc = new FileOutputStream(to).channel
oc.transferFrom(ic, 0, ic.size())
} finally {
if(ic) ic.close()
if(oc) oc.close()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment