Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write data bytes to a file at an offset in java

Tags:

java

file

I wish to write data to a file at different offsets. Example, at 0th position, at (size/2)th position, at (size/4)th position etc. size represent the file size of the file meant to be created. Is this possible without creating different file parts and joining them?

like image 837
John S John Avatar asked Mar 27 '13 07:03

John S John


1 Answers

Well you can write to anywhere you like in a file using RandomAccessFile - just use seek to get to the right place, and start writing.

However, this won't insert bytes at those places - it will just overwrite them (or add data at the end if you're writing past the end of the current file length, of course). It's not clear whether that's what you want or not.

like image 51
Jon Skeet Avatar answered Oct 05 '22 22:10

Jon Skeet