title: Modifying contents of a binary file in Java
body: <p>I'm writing a program in Java that I plan to use for modding a certain video game by modifying the game's binary data. It's stored in a .dat file that simply contains bytes of game data.  What I have been working with is <code>FileInputStream</code> to read in the file and <code>FileOutputStream</code> to modify the contents. However, as far as I am aware <code>FileOutputStream</code> is only capable of modifying the end of the file, not contents in the middle.</p>
  
  <p>What I want to be able to do the following:</p>
  
  <p>If this is a binary file provided:</p>
  
  <pre><code>06 B4 45 18 A7 DD
  </code></pre>
  
  <p>I would want to be able to, for instance, modify the byte at position n=3 and change it to value v=FF.</p>
  
  <p>resulting in:</p>
  
  <pre><code>06 B4 45 FF A7 DD
  </code></pre>
  
  <p>I'd like to write this data back to the file then.</p>
  
  <p>What would be the best way of tackling this? Right now, the only way I could see <code>FileOutputStream</code> working is if I actually took the original file, and reconstructed it from start to finish but swapping out the new values in the process. However, that seems really inefficient considering the files I plan to modify contain roughly 20,000 bytes each.</p>
  
  <p>I feel like there has to be an easier way. Any suggestions?</p>