20230328172812

从FileChannel中读取数据代码示例

RandomAccessFileaFile = new RandomAccessFile(fileName, "rw");
//获取通道 
FileChannelinChannel=aFile.getChannel(); 
//获取一个字节缓冲区 
ByteBufferbuf = ByteBuffer.allocate(CAPACITY); 
int length = -1; 
//调用通道的read方法,读取数据并买入字节类型的缓冲区 
while ((length = inChannel.read(buf)) ! = -1) { 
//省略……处理读取到的buf中的数据 
}