• Post Reply Bookmark Topic Watch Topic
  • New Topic

Buffering in RandomAccessFile

 
Alec Lee
Ranch Hand
Posts: 569
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is writing to RandomAccessFile automatically buffered? If not, is there any built-in stream that can wrap around it to provide buffering?

My concern is that I have to do many RandomAccessFile.write(int b) in a loop to fill out a big gap in a file. Big the performance of this operation is very slow so that I suspect each write() call causes a physical I/O. I know there is a write(byte[] b, int off, int len) version but I don't want to count the number of bytes written manually and just want to know if there is any ready-made solution.
 
Brian Mozhdehi
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is not way to cache the writing of bytes or buffer them directly using RandomAccessFile or FileOutputStream....but, there is more than likely an alternate solution.

Not sure what your code is doing, but based on nothing other than a gut reaction to what you are saying, why dont you write within the loop to a ByteArrayOutputStream and then at the END of the loop, write to the file? That way, there is only one I/O operation.
 
Alec Lee
Ranch Hand
Posts: 569
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

What i am trying to do is that I have to open a file, extract some info from the middle of it and fill out the part that has been extracted with 0x00. I want to simplify my code by a loop with write(0) only instead of manually writing the bytes to my own buffer and use write(byte[] buffer, int off, int len) - this would require me to count the # of bytes written and for the last write() call i need to make sure I just fill out the buffer with the remaining amount of bytes required (buffer size not integral multiple of the file size).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!