RandomAccessFile vs. FileChannel

C

Christopher Dean

Aside from memory mapped files for very large files, is there any
benefit between using the RandomAccessFile class and the FileChannel
class? They seem to be abstractions of the same low level I/O.
 
M

Michael Borgwardt

Christopher said:
Aside from memory mapped files for very large files, is there any
benefit between using the RandomAccessFile class and the FileChannel
class? They seem to be abstractions of the same low level I/O.

Quite different abstractions, though. Aside from memory mapping, FileChannel
allows you to do locking, and the access through Buffers is much better
suited to bulk operations.
 
C

Christopher Dean

Michael said:
Quite different abstractions, though. Aside from memory mapping,
FileChannel
allows you to do locking, and the access through Buffers is much better
suited to bulk operations.

Thanks. I am not concerned about locking. As for the buffering I know
they use the NIO buffers but I, and possibility incorrectly, assumed
that RandomAccessFile also has some buffer mechanism. If you are
telling me that every write on a RandomAccessFile goes directly to disk
then I will definitely switch to FileChannel for writing out my files.
 
M

Michael Borgwardt

Christopher said:
Thanks. I am not concerned about locking. As for the buffering I know
they use the NIO buffers but I, and possibility incorrectly, assumed
that RandomAccessFile also has some buffer mechanism. If you are
telling me that every write on a RandomAccessFile goes directly to disk
then I will definitely switch to FileChannel for writing out my files.

No, there is of course still some buffering done by the OS, but that's not
controllable by you, and you have more overhead in the Java layer as well,
especially when you want to read large numbers of primitives (int, float, etc.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top