I've created a file using java.io.File
, FileInputStream & FileOutputStream
. Suppose that I want to change the value of some bytes in the file (for instance from the byte 15 to 35) without changing the size of the file. I've tried creating a RandomAccessFile
object and then use RandomAccessFile.seek
to move to byte number 15, writing my new bytes and then closing the file. The file has changed its size. What's wrong with this approach, and how can this be done successfully?
Are you sure you are writing a byte to the RandomAccessFile? If you are calling the method:
file.write(35);
Then it is actually writing 35
as an int
which is 4 bytes. If you want to write a single byte try:
file.writeByte(35);