Java FileOutputStream.getChannel()方法範例

2019-10-16 22:14:25

FileOutputStream.getChannel()方法具有以下語法。

public FileChannel getChannel()

範例

在下面的程式碼顯示如何使用FileOutputStream.getChannel()方法。

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

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

    byte b[] = { 65, 66, 67, 68, 69 };

    FileOutputStream fos = new FileOutputStream("C://test.txt");

    fos.write(b);

    // pushes stream content to the underlying file
    fos.flush();

    // returns file channel associated with this stream
    FileChannel fc = fos.getChannel();

    // returns the number of bytes written
    long pos = fc.position();

    System.out.print(pos);

  }
}