Andrey Hihlovskiy

Professional blog on groovy, gradle, Java, Javascript and other stuff.

Fun with groovy files

The following one-liner can copy very large files without running out of memory:

new File("test").withInputStream { new File("test2") << it }

Just tested it in groovy console – 1 GB file is copied in 5 seconds, memory consumption stays low.
The copy is binary, i.e. it copies bytes, not chars.
One particularity: left-shift operator rather appends than overwrites. If you need to overwrite the file, first need to delete it.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: