【发布时间】:2016-06-17 02:51:35
【问题描述】:
我一次读取 510 个字节的文件。字节位于字节缓冲区中,我正在使用 fileChannel 读取它们。
一旦我改变了位置,它会再次检查 while 循环内的大小写,然后跳出 while 循环。总字节数约为 8000 字节。如何倒退到 fileChannel 中的特定位置而不会导致此错误?
这是我的代码:
File f = new File("./data.txt");
FileChannel fChannel = f.getChannel();
ByteBuffer bBuffer = ByteBuffer.allocate(510);
while(fChannel.read(bBuffer) > 0){
//omit code
if(//case){
fChannel.position(3060);
}
}
【问题讨论】:
-
如果你需要随机访问,我建议内存映射文件。您可以在 Java 中轻松地为最多 2 GB 的数据执行此操作(并且它不使用堆空间来存储数据)
标签: java byte bytebuffer filechannel