Java FileOutputStream.getChannel()

Syntax

FileOutputStream.getChannel() has the following syntax.

public FileChannel getChannel()

Example

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


/*w w  w.j a v  a2  s .  co m*/

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);

  }
}