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

时间:2016-09-20 20:14:13

标签: scala akka-stream

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

2 个答案:

答案 0 :(得分:3)

我认为最简单的方法是使用runFold

FileIO.fromPath(Paths.get("some file path"))
  .runFold(ByteString.empty)(_ ++ _)

这会返回Future[ByteString]

也就是说,只使用Files.readAllBytes加载整个文件,然后将生成的Array[Byte]转换为ByteString,效率会更高:

ByteString(Files.readAllBytes(Paths.get("some file path")))

答案 1 :(得分:0)

使用Source

等函数从map转换字节串
FileIO.fromPath(Paths.get("some file path")).map { byteString => 
  doSomething()
}