简体   繁体   English

使用FileChannel:Java从RandomAccessFile读取特定字节

[英]Read specific bytes from RandomAccessFile using FileChannel : Java

I have a RandomAccessFile and its FileChannel. 我有一个RandomAccessFile及其FileChannel。 What I'm trying to do is read a specific section of the bytes from said file; 我要做的是从所述文件中读取字节的特定部分; however, while looking over the FileChannel read methods, I didn't see overloads that would help with what I'm trying to do, only methods which would read the entire content, or read content to a specific position in the buffer that the read method takes in. Is there a method that I'm missing that will help me accomplish my goal of reading a specific chunk of bytes, or will I need to read in the entire channel into the buffer and then parse out the section somehow? 然而,在查看FileChannel读取方法时,我没有看到有助于我正在尝试做的重载,只有读取整个内容的方法,或者读取内容到读取缓冲区中的特定位置方法接受。是否有一个我缺少的方法可以帮助我实现读取特定字节块的目标,或者我需要在整个通道中读入缓冲区然后以某种方式解析该部分?

您可以使用FileChannel#position(long)转到文件中的特定位置,然后read所需大小的缓冲区。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM