简体   繁体   English

FileInputStream.read(...) 和 RandomAccessFile 的区别

[英]Difference between FileInputStream.read(...) and RandomAccessFile

I am new to Random File Access, and I have encountered one issue - as far as I have understood, RandomAccessFile class provides a Random Access file for reading/writing.我是随机文件访问的新手,我遇到了一个问题——据我所知, RandomAccessFile类提供了一个用于读/写的随机访问文件。 I can use seek() method to move to preferable position and start reading or wrting, but does not matter in this case.我可以使用seek()方法移动到首选位置并开始阅读或写作,但在这种情况下无关紧要。 It is completely the random access?完全是随机存取吗? But in FileInputStream I have the same ability但在FileInputStream我有同样的能力

read(bute[] byte, int off, int len)

this method provides me reading from some particular place.这种方法让我可以从某个特定的地方阅读。 So, what is the difference?那么区别是什么呢? (I guess, InputStream read all file, but just go through all symbols before off position, but it only my guess). (我猜, InputStream 读取所有文件,但只是在关闭位置之前遍历所有符号,但这只是我的猜测)。

Looking at the documentation of the read method: https://docs.oracle.com/javase/7/docs/api/java/io/FileInputStream.html#read(byte[],%20int,%20int) it states that off is "the start offset in the destination array b".查看read方法的文档: https : //docs.oracle.com/javase/7/docs/api/java/io/FileInputStream.html#read(byte[] ,%20int,% 20int)它指出off 是“目标数组 b 中的起始偏移量”。 So using this call, you can read the next len bytes from the stream and put them is a certain place in your memory buffer.因此,使用此调用,您可以从流中读取下一个len字节并将它们放在内存缓冲区中的某个位置。 This does not allow you to skip forward like the seek method of a random access file.这不允许您像随机访问文件的seek方法那样向前跳过。

The read method you mention does not let you read from any particular place.您提到的read方法不允许您从任何特定位置阅读。 It always reads from the "next" position in the stream, where it left off, and it puts the read bytes into the byte array at position off .它总是从流中的“下一个”位置读取,在那里它停止,并将读取的字节放入字节数组中的位置off off is the offset in the output , not the input. off输出中的偏移,而不是输入中的偏移量。

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

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