Java IO Tutorial - 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.

/*from   ww  w  .j a  va2s .  c o 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);

  }
}