Under what circumstances would I use random access I/O over sequential, buffered I/O?

John Zukowski

Whether you use random access I/O or sequential access really depends upon what you are trying to do. Random access I/O is usually used for fixed-size data records, where you want to overwrite the original record with changes, or to create something like a rolling log file. Nowadays, there are lightweight database systems that would do this type of operation for you, adding capabilities like querying and a standard JDBC access so you wouldn't have to waste your time redesigning the wheel. While not trying to tell you to never use random access I/O, the Java RandomAccessFile class lives outside the Java streams class hierarchy meaning that you can't add a facade around the random access file to buffer it or enrich its capabilities in any manner. And you would also need to program in things like simultaneous access.

Sequential access is just for that, when you need to access a file sequentially from start to finish. While you can skip() around the data, it is generally meant to be read from beginning to end, where the whole file has some meaning. For performance reasons, it is best to always buffer your I/O, at least when using Reader, Writer, InputStream, and OutputStream classes, but not RandomAccessFile.

0 Comments  (click to add your comment)
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact