RandomAccessFile vs. FileChannel  
Author Message
Christopher Dean





PostPosted: 2003-11-19 2:09:00 Top

java-programmer, RandomAccessFile vs. FileChannel 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.

 
Michael Borgwardt





PostPosted: 2003-11-19 17:40:00 Top

java-programmer >> RandomAccessFile vs. FileChannel Christopher Dean wrote:
> 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.

 
Christopher Dean





PostPosted: 2003-11-20 1:09:00 Top

java-programmer >> RandomAccessFile vs. FileChannel Michael Borgwardt wrote:
> Christopher Dean wrote:
>
>> 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.
>

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.

 
 
Michael Borgwardt





PostPosted: 2003-11-20 1:22:00 Top

java-programmer >> RandomAccessFile vs. FileChannel Christopher Dean wrote:
>> 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.

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.)