改变一个特定字节在一个文件字节、文件

2023-09-04 23:35:25 作者:Poison 毒药

我想写一个java功能,这将改变1个字节的大文件。我怎样才能读取和写入到特定地址中使用Java在Android上的文件?我曾尝试fis.read(BYTE B [],整数关,INT LEN),我每次都遇到一个强制关闭。

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.

推荐答案

使用RandomAccessFile.

开球例如:

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.
}