如何以惯用的方式从 FileIO.fromPath 获取 Bytestring

Rab*_*bzu 1 scala akka-stream

在 Scala 中从 FileIO.fromPath 获取 akka Bytestring 的惯用方法是什么?

Vla*_*eev 5

我相信最简单的方法是使用runFold

FileIO.fromPath(Paths.get("some file path"))
  .runFold(ByteString.empty)(_ ++ _)
Run Code Online (Sandbox Code Playgroud)

这将返回一个Future[ByteString].

也就是说,很可能只加载整个文件Files.readAllBytes,然后将结果转换Array[Byte]为 aByteString会更有效:

ByteString(Files.readAllBytes(Paths.get("some file path")))
Run Code Online (Sandbox Code Playgroud)