获取文件后半部分的输入流的最佳方法?
我需要一个InputStream
来从File
中的某个非零偏移量读取。哪种是获取和定位流的更高效的方法,
InputStream in = new FileInputStream(file);
in.skip(n);
或者
RandomAccessFile raf = new RandomAccessFile(file, "r");
InputStream in = Channels.newInputStream(raf.getChannel().position(n));
您建议有更好的方法?
只会从文件中提取一个流,因此重用 RandomAccessFile
没有任何好处。
I need an InputStream
to read from some nonzero offset in a File
. Which is the more performant way to get and position the stream,
InputStream in = new FileInputStream(file);
in.skip(n);
or
RandomAccessFile raf = new RandomAccessFile(file, "r");
InputStream in = Channels.newInputStream(raf.getChannel().position(n));
Or is there a better way that you'd suggest?
Only one stream will be pulled from the file, so there's no benefit from reusing the RandomAccessFile
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在幕后,我相信这些方法以完全相同的方式工作(对于覆盖 Skip 的默认实现的 FileInputStreams)。因此,性能上没有明显差异。
您可能对其他相关问题感兴趣。
Behind the scenes I believe the methods work in exactly the same way (for FileInputStreams which override the default implementation of skip). And thus there being no appreciable difference in performance.
You may be in interested in this other related question.