title: Search for a String (as an byte[]) in a binary stream
body: <p><p>Hi Team, I am trying to find a String "Henry" in a binary file and change the String  to a different string. FYI the file is the output of serialisation of an object. <a href="https://stackoverflow.com/questions/22232096/how-to-i-read-and-edit-txt-in-a-serialised-file">Original Question here</a>
  <p>
  I am new to searching bytes and imagined this code would search for my byte[] and exchange it. But it doesn't come close to working it doesn't even find a match.</p>
  
  <pre><code>{
      byte[] bytesHenry = new String("Henry").getBytes();
      byte[] bytesSwap = new String("Zsswd").getBytes();
  
      byte[] seekHenry = new byte[bytesHenry.length];
  
      RandomAccessFile file = new RandomAccessFile(fileString,"rw");
  
      long filePointer;
      while (seekHenry != null) {
         filePointer = file.getFilePointer();
         file.readFully(seekHenry);
         if (bytesHenry == seekHenry) {
             file.seek(filePointer);
             file.write(bytesSwap);
             break;
         }
       }
  }
  </code></pre>
  
  <hr>
  
  <p>Okay I see the <code>bytesHenry==seekHenry</code> problem and will swap to <code>Arrays.equals( bytesHenry , seekHenry )</code><p></p>
  
  <p>I think I need to move along by -4 byte positions each time i read 5 bytes.</p>
  
  <hr>
  
  <p>Bingo it finds it now</p>
  
  <pre><code>    while (seekHenry != null) {
                      filePointer = file.getFilePointer();
                      file.readFully(seekHenry);;
                      if (Arrays.equals(bytesHenry,
                                        seekHenry)) {
                          file.seek(filePointer);
                          file.write(bytesSwap);
                          break;
                      }
                      file.seek(filePointer);
                      file.read();
                  }
  </code></pre>