0
將多個文件的內容追加到vertx中的單個文件中的最佳方法是什麼?我已經嘗試vertx 文件系統和asyncFile但都沒有選擇追加文件或我不知道任何。是否有任何其他方法異步合併或追加vertx文件。Vertx合併單個文件中多個文件的內容
我能找到的唯一解決方案是使用循環將緩衝區列表寫入每個先前緩衝區長度的末尾。
將多個文件的內容追加到vertx中的單個文件中的最佳方法是什麼?我已經嘗試vertx 文件系統和asyncFile但都沒有選擇追加文件或我不知道任何。是否有任何其他方法異步合併或追加vertx文件。Vertx合併單個文件中多個文件的內容
我能找到的唯一解決方案是使用循環將緩衝區列表寫入每個先前緩衝區長度的末尾。
事實上,從Vert.x 3.4開始,在FileSystem
上沒有幫助方法來將文件附加到另一個文件。
您可以使用AsyncFile
和Pump
如下。
首先創建一個實用的方法來打開文件:
Future<AsyncFile> openFile(FileSystem fileSystem, String path, OpenOptions openOptions) {
Future<AsyncFile> future = Future.future();
fileSystem.open(path, openOptions, future);
return future;
}
然後一個又一個文件到另一個文件追加了Pump
:
Future<AsyncFile> append(AsyncFile source, AsyncFile destination) {
Future<AsyncFile> future = Future.future();
Pump pump = Pump.pump(source, destination);
source.exceptionHandler(future::fail);
destination.exceptionHandler(future::fail);
source.endHandler(v -> future.complete(destination));
pump.start();
return future;
}
現在你可以將那些順序組合:
void merge(FileSystem fileSystem, String output, List<String> sources) {
openFile(fileSystem, output, new OpenOptions().setCreate(true).setTruncateExisting(true).setWrite(true)).compose(outFile -> {
Future<AsyncFile> mergeFuture = null;
for (String source : sources) {
if (mergeFuture == null) {
mergeFuture = openFile(fileSystem, source, new OpenOptions()).compose(sourceFile -> {
return append(sourceFile, outFile);
});
} else {
mergeFuture = mergeFuture.compose(v -> {
return openFile(fileSystem, source, new OpenOptions()).compose(sourceFile -> {
return append(sourceFile, outFile);
});
});
}
}
return mergeFuture;
}).setHandler(ar -> {
System.out.println("Done");
});
}
追加asynchro意味着你不關心訂單文件將被追加。你的情況正確嗎? –