Created
December 6, 2012 21:24
-
-
Save jarek-przygodzki/4228609 to your computer and use it in GitHub Desktop.
Truncate a file
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.channels.* | |
def truncFile(File file, long newSize) { | |
FileChannel fileChannel; | |
try { | |
fileChannel = new FileOutputStream(file, true).getChannel(); | |
fileChannel.truncate(newSize); | |
} finally { | |
fileChannel?.close() | |
} | |
} |
Very elegant! Thank you both.
Thanks. Great snippet.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How about this:
def truncFile(File file, long newSize) {
new FileOutputStream(file).withStream{
it.channel.truncate(newSize)
}
}