Example usage for io.vertx.core.file AsyncFile write

List of usage examples for io.vertx.core.file AsyncFile write

Introduction

In this page you can find the example usage for io.vertx.core.file AsyncFile write.

Prototype

void write(Buffer buffer, long position, Handler<AsyncResult<Void>> handler);

Source Link

Document

Write a io.vertx.core.buffer.Buffer to the file at position position in the file, asynchronously.

Usage

From source file:examples.FileSystemExamples.java

License:Open Source License

public void asyncFileWrite() {
    Vertx vertx = Vertx.vertx();/*from  w ww .  j  a v  a  2 s .co  m*/
    vertx.fileSystem().open("target/classes/hello.txt", new OpenOptions(), result -> {
        if (result.succeeded()) {
            AsyncFile file = result.result();
            Buffer buff = Buffer.buffer("foo");
            for (int i = 0; i < 5; i++) {
                file.write(buff, buff.length() * i, ar -> {
                    if (ar.succeeded()) {
                        System.out.println("Written ok!");
                        // etc
                    } else {
                        System.err.println("Failed to write: " + ar.cause());
                    }
                });
            }
        } else {
            System.err.println("Cannot open file " + result.cause());
        }
    });
}