Search code examples
javaiofileinputstream

java.io.FileInputStream.getChannel() Method


I'm working around I/O and found java.io.FileInputStream.getChannel() on the internet. I want to know the exact purpose of getChannel. Why do we need to use java.io.FileInputStream.getChannel()?

Example: http://www.tutorialspoint.com/java/io/fileinputstream_getchannel.htm


Solution

  • By creating the channel,The stream will be safe for access by multiple concurrent threads.

    And from FileChannel class:

    File channels are safe for use by multiple concurrent threads. The close method may be invoked at any time, as specified by the Channel interface. Only one operation that involves the channel's position or can change its file's size may be in progress at any given time; attempts to initiate a second such operation while the first is still in progress will block until the first operation completes. Other operations, in particular those that take an explicit position, may proceed concurrently; whether they in fact do so is dependent upon the underlying implementation and is therefore unspecified.