Vert.x 3学习笔记---09

Using the file system with Vert.x
vertx的FileSystem 对象提供很多的便捷操作。
每一个vertx的实例拥有一个FileSystem 对象的实例。
每一个操作都提供了阻塞和非阻塞的版本。非阻塞版本的操作使用一个handler来处理完成或者失败的结果。

FileSystem fs = vertx.fileSystem();

// Copy file from foo.txt to bar.txt
fs.copy("foo.txt", "bar.txt", res -> {
    if (res.succeeded()) {
        // Copied ok!
    } else {
        // Something went wrong
    }
});

同步版本的操作的方法名命名:xxxBlocking,直接返回结果,或者直接抛出异常。

FileSystem fs = vertx.fileSystem();

// Copy file from foo.txt to bar.txt synchronously
fs.copyBlocking("foo.txt", "bar.txt");

较完整的例子:

Vertx vertx = Vertx.vertx();

// Read a file
vertx.fileSystem().readFile("target/classes/readme.txt", result -> {
    if (result.succeeded()) {
        System.out.println(result.result());
    } else {
        System.err.println("Oh oh ..." + result.cause());
    }
});

// Copy a file
vertx.fileSystem().copy("target/classes/readme.txt", "target/classes/readme2.txt", result -> {
    if (result.succeeded()) {
        System.out.println("File copied");
    } else {
        Sys

你可能感兴趣的:(vertx,vert.x,nodejs,async-file)