1

In Java using RandomAccessFile I can create a file and then do setLength Now Assume I allocate length of 1024 bytes. Now I wrote say 512 bytes. Now again I want to write 256 bytes without changing size of the file and clearing those 512 bytes

I do not want to use setLength(0) in the middle of my process to avoid java.io.IOException: No space left on device

Avinash
  • 12,851
  • 32
  • 116
  • 186

1 Answers1

4

You can use seeks:

file.setLength(1024);
file.write(new byte[512]); //or some other 512 bytes
//close and reopen later
file.seek(512);
//write 256 more bytes

Edit: I don't think you're realizing the spirit of RandomAccessFile. You can create it to be 512 bytes and keep writing to it so it can extend implicitly without setLength calls.

nanofarad
  • 40,330
  • 4
  • 86
  • 117