0

Im currently trying to build a save editor for a video game. Anyway the I figured out how to write to the binary file with output stream rather than writer I'm running into a problem. I'm trying to overwrite certain hexadecimal values but every time I try I end up replacing the whole file, theres probably an easy explanation for this but I also wanted advice on how to replace the hex values converting the hex values (ex. 5acd) from a string only gives me the byte data for the strings. Heres what I'm doing:

    String textToWrite = inputField.getText();
    byte[] charsToWrite = textToWrite.getBytes();

    FileOutputStream out = new FileOutputStream(theFile);


    out.write(charsToWrite, 23, charsToWrite.length)
0xOffset
  • 45
  • 1
  • 6
  • did you check this out? http://stackoverflow.com/questions/289965/inserting-text-into-an-existing-file-via-java – Murali VP Dec 10 '11 at 02:43
  • yes, I did it was a little different than what I was asking but gave me a little more insight. Im interested in binary manipulation. – 0xOffset Dec 10 '11 at 03:49

2 Answers2

1

Use a RandomAccessFile. This has the methods that you are looking for. FileOutputStream will only allow you to overwrite or append. However, note as Murali VP eluded to, this will only allow you to perform direct replacements (byte-for-byte) - and not removal or insertion of bytes.

ziesemer
  • 27,712
  • 8
  • 86
  • 94
0

Converting from Hex String to Byte Array (which is essentially what you need) - see this SO post for what you need.

HTH

Community
  • 1
  • 1
Crollster
  • 2,751
  • 2
  • 23
  • 34