Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

FileChannel vs RandomAccessFile  RSS feed

 
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: 10573
65
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: 10573
65
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
Saloon Keeper
Posts: 6980
110
  • 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
Sheriff
Posts: 53752
127
  • 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!