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 {
System.err.println("Oh oh ..." + result.cause());
}
});
// Write a file
vertx.fileSystem().writeFile("target/classes/hello.txt", Buffer.buffer("Hello"), result -> {
if (result.succeeded()) {
System.out.println("File written");
} else {
System.err.println("Oh oh ..." + result.cause());
}
});
// Check existence and delete
vertx.fileSystem().exists("target/classes/junk.txt", result -> {
if (result.succeeded() && result.result()) {
vertx.fileSystem().delete("target/classes/junk.txt", r -> {
System.out.println("File deleted");
});
} else {
System.err.println("Oh oh ... - cannot delete the file: " + result.cause());
}
});
异步操作文件
OpenOptions options = new OpenOptions();
fileSystem.open("myfile.txt", options, res -> {
if (res.succeeded()) {
AsyncFile file = res.result();//读取的文件内容
} else {
// Something went wrong!
}
});
AsyncFile implements ReadStream and WriteStream so you can pump files to and from other stream objects such as net sockets, http requests and responses, and WebSockets.
随机写
To use an AsyncFile for random access writing you use the write method.
The parameters to the method are:
buffer: the buffer to write.
position: an integer position in the file where to write the buffer. If the position is greater or equal to the size of the file, the file will be enlarged to accommodate the offset.
handler: the result handler
Here is an example of random access writes:
Vertx vertx = Vertx.vertx();
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());
}
});
随机读
To use an AsyncFile for random access reads you use the read method.
The parameters to the method are:
buffer: the buffer into which the data will be read.
offset: an integer offset into the buffer where the read data will be placed.
position: the position in the file where to read data from.
length: the number of bytes of data to read
handler: the result handler
Here’s an example of random access reads:
Vertx vertx = Vertx.vertx();
vertx.fileSystem().open("target/classes/les_miserables.txt", new OpenOptions(), result -> {
if (result.succeeded()) {
AsyncFile file = result.result();
Buffer buff = Buffer.buffer(1000);
for (int i = 0; i < 10; i++) {
file.read(buff, i * 100, i * 100, 100, ar -> {
if (ar.succeeded()) {
System.out.println("Read ok!");
} else {
System.err.println("Failed to write: " + ar.cause());
}
});
}
} else {
System.err.println("Cannot open file " + result.cause());
}
});
当我们使用AsyncFile,会使用OpenOptions对象。这个对象描述了文件访问的行为。例如:你可以设置文件的权限:通过setRead, setWrite and setPerms methods来设置可读,可写,可执行操作。
flush方法会将数据写入到底层。也可以setDsync,表示每一次执行write操作后,都会自动的写入到底层。
AsyncFile implements ReadStream and WriteStream
Vertx vertx = Vertx.vertx();
final AsyncFile output = vertx.fileSystem().openBlocking("target/classes/plagiary.txt", new OpenOptions());
vertx.fileSystem().open("target/classes/les_miserables.txt", new OpenOptions(), result -> {
if (result.succeeded()) {
AsyncFile file = result.result();
Pump.pump(file, output).start();
file.endHandler((r) -> {
System.out.println("Copy done");
});
} else {
System.err.println("Cannot open file " + result.cause());
}
});
访问classpath下的文件
在vertx中,classpath下的资源的路径不要以“/”开头。
This caching behaviour can be disabled by setting the system property vertx.disableFileCaching to true. The path where the files are cached is .vertx by default and can be customized by setting the system property vertx.cacheDirBase.
文件缓存默认是关闭的。开启文件缓存使用vertx.disableFileCaching(true)
。文件缓存的地址默认是.vertx,也可以通过vertx.cacheDirBase来更改。
The whole classpath resolving feature can be disabled by setting the system property vertx.disableFileCPResolving to true.这个地方没懂!!!
To close an AsyncFile call the close method. Closing is asynchronous and if you want to be notified when the close has been completed you can specify a handler function as an argument.