tags:

views:

144

answers:

2

What is the best way to change a single byte in a file using Java? I've implemented this in several ways. One uses all byte array manipulation, but this is highly sensitive to the amount of memory available and doesn't scale past 50 MB or so (i.e. I can't allocate 100MB worth of byte[] without getting OutOfMemory errors). I also implemented it another way which works, and scales, but it feels quite hacky.

If you're a java io guru, and you had to contend with very large files (200-500MB), how might you approach this?

Thanks!

+14  A: 

I'd use RandomAccessFile, seek to the position I wanted to change and write the change.

Cameron Pope
+5  A: 

If all I wanted to do was change a single byte, I wouldn't bother reading the entire file into memory. I'd use a RandomAccessFile, seek to the byte in question, write it, and close the file.

Eddie