[例] 用MappedByteBuffer更新文件内容

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;


public class Test_00 {


public static void main(String[] args) throws IOException {
test1();

}


public static void test1() throws IOException {
RandomAccessFile fis = new RandomAccessFile("C:\test\test3.ini", "rw");
FileChannel fci = fis.getChannel();
MappedByteBuffer mbb = fci.map(FileChannel.MapMode.READ_WRITE, 0,
fis.length());

System.out.println(mbb.position());

byte dst[] = new byte[(int) fis.length()];
mbb.get(dst);
System.out.println(mbb.position());

System.out.println(new String(dst));

dst[0] = 'X';
dst[1] = 'Y';
dst[2] = 'Z';

mbb.position(0);
mbb.put(dst);

fis.close();

}
}

原文地址:https://www.cnblogs.com/pekkle/p/6568823.html