Skip to content

File vs RandomAccessFile

TIP

File 为顺序读取,RandomAccessFile可以任意读取。

区别

如果想跳过前100K后,再获取5K的数据。

File需要先读取前100K,再读取5K数据;

RandomAccessFile可以直接跳到第101K,读取5K数据。

引用&参考

A random access file is a file where you can "jump" to anywhere within it without having to read sequentially until the position you are interested in. For example, say you have a 1MB file, and you are interested in 5 bytes that start after 100k of data. A random access file will allow you to "jump" to the 100k-th position in one operation. A non-random access file will require you to read 100k bytes first, and only then read the data you're interested in. Hope that helps. Clarification: this description is language-agnostic and does not relate to any specific file wrapper in any specific language/framework.

https://stackoverflow.com/questions/905706/what-is-the-difference-between-file-and-random-access-file/905720#905720?newreg=77397c424daf496884332c3f9f57d051