• Post Reply Bookmark Topic Watch Topic
  • New Topic

FileChannel vs RandomAccessFile

 
Nick Widelec
Ranch Hand
Posts: 226
Eclipse IDE Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Considering that FileChannel in Java 7 implements the interface SeekableByteChannel. Is there any use in using RandomAccessFile rather than FileChannel? Thanks in advance.
 
Winston Gutkowski
Bartender
Posts: 10571
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nick Widelec wrote:Considering that FileChannel in Java 7 implements the interface SeekableByteChannel. Is there any use in using RandomAccessFile rather than FileChannel? Thanks in advance.

Well, given that one defintion is an awful lot bigger than the other, and allows you to read and write most major types directly, I'd say: yes.

Winston
 
Nick Widelec
Ranch Hand
Posts: 226
Eclipse IDE Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for replying.
Could you please show me a case where FileChannel cannot reach the same function of a RandomAccessFile?
 
Winston Gutkowski
Bartender
Posts: 10571
64
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nick Widelec wrote:Could you please show me a case where FileChannel cannot reach the same function of a RandomAccessFile?

Sure: try readFloat() with a FileChannel.

Winston

 
Nick Widelec
Ranch Hand
Posts: 226
Eclipse IDE Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
from eclipse API:
readFloat();
...

"This method blocks until the four bytes are read, the end of the stream is detected, or an exception is thrown."

What does "this method blocks" mean? FileChannel methods do not block? What's the concept of blocking here? Thanks a lot in advance.

 
Stephan van Hulst
Bartender
Posts: 6583
84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Blocking refers to that the method call will appear to "stop working" until there is some input available. It makes it easier for you to write a program, because when the method returns you will be guaranteed to have a value. Non-blocking methods will return immediately and either return a special value if no data is available, throw an exception, or notify a listener at a later point when a value is available.
 
Campbell Ritchie
Marshal
Posts: 52519
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Too difficult a question for “beginning”, so I shall move it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!