Java FileInputStream.getChannel()方法示例

广告位

Java FileInputStream.getChannel()方法示例 FileInputStream的F…

Java FileInputStream.getChannel()方法示例

FileInputStreamFileInputStream.getChannel()方法具有以下语法。

public FileChannel getChannel()  

示例

在下面的代码显示如何使用FileInputStream.getChannel()方法。

  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 // By: W  w W.Y I iba I.c  o  M        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);      }    }  }  

贺, 贺朝

关于作者: 贺朝

为您推荐

广告位

发表评论