Java 中有没有办法以异步方式将一个文件复制到另一个文件中?类似于 Stream.CopyToAsync在 C# 中是我想要找到的。
我想要实现的是从 Internet 下载一系列 ~40 个文件,这是我为每个文件想出的最好的:
CompletableFuture.allOf(myFiles.stream()
.map(file -> CompletableFuture.supplyAsync(() -> syncDownloadFile(file)))
.toArray(CompletableFuture[]::class))
.then(ignored -> doSomethingAfterAllDownloadsAreComplete());
哪里
syncDownloadFile
是:private void syncDownloadFile(MyFile file) {
try (InputStream is = file.mySourceUrl.openStream()) {
long actualSize = Files.copy(is, file.myDestinationNIOPath);
// size validation here
} catch (IOException e) {
throw new RuntimeException(e);
}
}
但这意味着我在任务执行程序内部有一些阻塞调用,我想避免这种情况,所以我不会一次阻塞太多执行程序。
我不确定 C# 方法是否在内部执行相同的操作(我的意思是,必须下载该文件,对吗?)。
有没有更好的方法来实现这一点?
最佳答案
AsynchronousFileChannel
(简称 AFC)是在 Java 中使用非阻塞 IO 管理文件的正确方法。不幸的是,它没有提供基于 promise (在 .net 中也称为 Task
)的 API,例如 CopyToAsync(Stream)
.Net 的。
替代方案 RxIo库建立在 AFC 之上并提供 AsyncFiles
具有不同调用习惯的异步 API:基于回调,CompletableFuture
(相当于 .net Task
)以及 react 流。
例如,可以异步地从一个文件复制到另一个文件:
Path in = Paths.get("input.txt");
Path out = Paths.get("output.txt");
AsyncFiles
.readAllBytes(in)
.thenCompose(bytes -> AsyncFiles.writeBytes(out, bytes))
.thenAccept(index -> /* invoked on completion */)
请注意,continuation 是由后台线程调用的
AsynchronousChannelGroup
.因此,您可以使用非阻塞 HTTP 客户端解决您的问题,使用
ComplableFuture
与 AsyncFiles
链接的基于 API用。例如,AHC 是有效的选择。在此处查看用法:https://github.com/AsyncHttpClient/async-http-client#using-continuations
关于java - Java 中的异步 File.copy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47209167/