Vert.x 3学习笔记---09
2016-02-24 15:55
393 查看
Using the file system with Vert.x
vertx的FileSystem 对象提供很多的便捷操作。
每一个vertx的实例拥有一个FileSystem 对象的实例。
每一个操作都提供了阻塞和非阻塞的版本。非阻塞版本的操作使用一个handler来处理完成或者失败的结果。
同步版本的操作的方法名命名:xxxBlocking,直接返回结果,或者直接抛出异常。
较完整的例子:
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:
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:
flush方法会将数据写入到底层。也可以setDsync,表示每一次执行write操作后,都会自动的写入到底层。
在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.
文件缓存默认是关闭的。开启文件缓存使用
The whole classpath resolving feature can be disabled by setting the system property vertx.disableFileCPResolving to true.这个地方没懂!!!
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()); } });
Asynchronous files
异步操作文件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.
Random access writes
随机写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()); } });
Random access reads
随机读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()); } });
Opening Options
当我们使用AsyncFile,会使用OpenOptions对象。这个对象描述了文件访问的行为。例如:你可以设置文件的权限:通过setRead, setWrite and setPerms methods来设置可读,可写,可执行操作。flush方法会将数据写入到底层。也可以setDsync,表示每一次执行write操作后,都会自动的写入到底层。
Using AsyncFile as ReadStream and WriteStream
AsyncFile implements ReadStream and WriteStreamVertx 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()); } });
Accessing files from the classpath
访问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.这个地方没懂!!!
Closing an AsyncFile
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.相关文章推荐
- Android 欢迎引导页的魅力
- TCP/IP协议详解:卷一----笔记(1)概述
- Android 快速开发框架:推荐10个框架
- C#创建唯一的订单号, 考虑时间因素
- memcached的缺点
- 1143 多少个Fibonacci数
- Windows下编译最新volley jar包
- python beautifulsoup简单用法
- iOS Implicit declaration of function XXX is invalid in C99”
- matlab的parcorr函数
- 《二月英语激情飞扬》
- CREATE TABLE 表名 AS SELECT 语句
- ubuntu android jdk问题
- angularJs学习笔记-路由
- nw.js node-webkit系列(4)Native UI API 概述和应用规范
- 在Viewpager中的Fragment使用ContextMenu问题
- HttpURLConnection类
- 查詢yum install 安裝軟件的路徑
- 分页索引
- linux whereis命令查找文件位置