If you have a FileOutputStream
, regardless of how you created it, it’s easy to truncate the associated file to a zero length:
fileOutputStream.getChannel().truncate(0);
Then the file is empty and you write new contents to it.
It’s also possible to do a real overwrite, i.e. keeping old contents at regions you don’t write to:
fileOutputStream.getChannel().position(0);
Then the next writes go to the specified position overwriting as much bytes as you really write, but retaining all other bytes.