Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

NIO and updating a file region  RSS feed

 
Michael Remijan
Author
Ranch Hand
Posts: 131
7
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have (what I thought would be) a simple problem. I have a data file that needs to be updated. For example, I have a 100 byte array with the up-to-date information and I want to replace or update bytes 450 - 550 in the file with the new 100 bytes. When I set the postion of the file to byte 450 then call channel.write([]) the data gets *inserted*. I want to *update* it. Any suggestions?
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As the description of the API it seems that FileChannel.map(mode, position, size) might help. Please tell me if it does.
Though if the file is not large reading it entirely in memory would be better.
Also consider that

A mapped byte buffer and the file mapping that it represents remain valid until the buffer itself is garbage-collected.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!