[例] 用MappedByteBuffer更新文件内容

来源:互联网 发布:刷话费软件 编辑:程序博客网 时间:2024/06/05 18:58
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();}}

0 0