4

I recently came across FileChannel, I am a big fan of RandomAccessFile. But I am wondering why would I pick FileChannel over RandomAccessFile for reading from a file and writing that content to another.

Is there any specific performance reason? I dont want to use locking of FileChannel for any purpose as I believe that could be one of the reasons why filechannel can be used. I don't want to use BufferReader or anything like that as suggested in other StackOverflow response.

Community
  • 1
  • 1
fscore
  • 2,567
  • 7
  • 40
  • 74
  • Why? Well, *Currently I am using `transferTO` method of `FileChannel `to read and copy* is one good reason. – Elliott Frisch Nov 27 '14 at 05:13
  • I mean I am not using it but eventually would like to but not sure how to identify if that is better than RAF – fscore Nov 27 '14 at 05:15

5 Answers5

0

FileChannel API says: A region of a file may be mapped directly into memory; for large files this is often much more efficient than invoking the usual read or write methods.

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275
  • 1
    But they mention about the filechannel's read and write itself instead of any other read or write methods – fscore Nov 27 '14 at 05:18
0

There is nothing to choose between them unless you use a FileChannel with direct buffers and never access the data yourself, e.g. you only copy it to a SocketChannel. This is faster because data never has to cross the JNI/JVM boundary.

But I am wondering why you don't pick BufferedReader. It will certainly be orders of magnitude faster than either of these for reading a file line by line.

user207421
  • 305,947
  • 44
  • 307
  • 483
-1

RandomAccessFile is good in performance plus it allows to read and write most major types directly.

Du-Lacoste
  • 11,530
  • 2
  • 71
  • 51
-2

FileChannels are safe for use by multiple concurrent threads.

Shiva Krish
  • 109
  • 1
  • 7
-2

RandomAccessFile source:

See that RandomAccessFile is actually using FileChannel under the hood...

public final FileChannel getChannel() {
         synchronized (this) {
             if (channel == null) {
                 channel = FileChannelImpl.open(fd, true, rw, this);

                 /*
                  * FileDescriptor could be shared by FileInputStream or
                  * FileOutputStream.
                  * Ensure that FD is GC'ed only when all the streams/channels
                  * are done using it.
                  * Increment fd's use count. Invoking the channel's close()
                  * method will result in decrementing the use count set for
                  * the channel.
                  */
                 fd.incrementAndGetUseCount();
             }
             return channel;
         }
     }

http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/io/RandomAccessFile.java

Robert Christian
  • 18,218
  • 20
  • 74
  • 89
  • 5
    This is incorrect. FileChannel is not used internally within RandomAccessFile and is only initialised is getChannel is called. Native methods are used to open/read/write the underlying file. – Mark Feb 23 '16 at 12:36
  • 2
    It is provided as a public method for anyone to get reference to the FileChannel for the underlying file. – Ronn Macc May 31 '18 at 06:40