Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing a specific byte in a file

I am trying to write a java function that will change 1 byte in a large file. How can I read in and write to a specific address in a file with java on android? I have tried fis.read(byte b[], int off, int len) and I get a force close every time.

like image 221
DaGentooBoy Avatar asked Jan 01 '11 23:01

DaGentooBoy


People also ask

What is file byte?

Each Byte File System is a mountable file system. The root file system is the first file system mounted. Subsequent file systems can be mounted on any directory within the root file system or on a directory within any mounted file system.


1 Answers

Use RandomAccessFile.

Kickoff example:

RandomAccessFile raf = new RandomAccessFile(file, "rw");
try {
    raf.seek(5); // Go to byte at offset position 5.
    raf.write(70); // Write byte 70 (overwrites original byte at this offset).
} finally {
    raf.close(); // Flush/save changes and close resource.
}
like image 200
BalusC Avatar answered Sep 30 '22 17:09

BalusC