Java IO Tutorial - Java FileInputStream.getChannel()








Syntax

FileInputStream.getChannel() has the following syntax.

public FileChannel getChannel()

Example

In the following code shows how to use FileInputStream.getChannel() method.

//from w  w w  .ja va 2s. c  o m


import java.io.FileInputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

public class Main {
  public static void main(String[] args) throws IOException {

    int i = 0;

    FileInputStream fis = new FileInputStream("C://test.txt");

    // read till the end of the file
    while ((i = fis.read()) != -1) {
      // get file channel
      FileChannel fc = fis.getChannel();

      // get channel position
      long pos = fc.position();

      char c = (char) i;

      System.out.println("No of bytes read: " + pos);
      System.out.println("Char read: " + c);
    }
  }
}