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();
}
}[例] 用MappedByteBuffer更新文件内容
最新推荐文章于 2025-09-04 07:00:00 发布
本文介绍了一个使用Java NIO进行文件映射的例子,通过RandomAccessFile和FileChannel实现对文件的读写操作,并展示了如何将文件映射到内存中进行高效处理。
开发板推荐:天空星STM32F407VET6开发板
超高性价比 STM32主控 | 超高主频 | 一板兼容百芯 | 比赛神器 | 沉金彩色丝印
开发板推荐:天空星STM32F407VET6开发板
超高性价比 STM32主控 | 超高主频 | 一板兼容百芯 | 比赛神器 | 沉金彩色丝印

471

被折叠的 条评论
为什么被折叠?



