File truncate operation in Java

Use FileChannel.truncate:

try (FileChannel outChan = new FileOutputStream(f, true).getChannel()) {
  outChan.truncate(newSize);
}

Leave a Comment