Example usage for java.nio.channels AsynchronousFileChannel write

List of usage examples for java.nio.channels AsynchronousFileChannel write

Introduction

In this page you can find the example usage for java.nio.channels AsynchronousFileChannel write.

Prototype

public abstract Future<Integer> write(ByteBuffer src, long position);

Source Link

Document

Writes a sequence of bytes to this channel from the given buffer, starting at the given file position.

Usage

From source file:Test.java

public static void main(String[] args) throws Exception {
    AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(Paths.get("/asynchronous.txt"),
            StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE);

    Future<Integer> writeFuture1 = fileChannel.write(ByteBuffer.wrap("Sample".getBytes()), 0);
    Future<Integer> writeFuture2 = fileChannel.write(ByteBuffer.wrap("Box".getBytes()), 0);

    int result;//w  ww .  j av  a2 s  .  c  om
    result = writeFuture1.get();
    System.out.println("Sample write completed with " + result + " bytes written");
    result = writeFuture2.get();
    System.out.println("Box write completed with " + result + " bytes written");

}