Java FileInputStream.getChannel()方法範例
FileInputStream
的FileInputStream.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);
}
}
}