Created
March 29, 2010 11:49
-
-
Save marcelmaatkamp/347776 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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