如何以惯用的方式从 FileIO.fromPath 获取字节串
How to get Bytestring from FileIO.fromPath in an idiomatic way
在 Scala 中从 FileIO.fromPath 获取 akka Bytestring 的惯用方法是什么?
使用 map
等函数从 Source
转换字节串
FileIO.fromPath(Paths.get("some file path")).map { byteString =>
doSomething()
}
我认为最简单的方法是使用 runFold
:
FileIO.fromPath(Paths.get("some file path"))
.runFold(ByteString.empty)(_ ++ _)
这个returns一个Future[ByteString]
.
也就是说,很可能只是用 Files.readAllBytes
加载整个文件,然后将结果 Array[Byte]
转换为 ByteString
会更有效率:
ByteString(Files.readAllBytes(Paths.get("some file path")))
在 Scala 中从 FileIO.fromPath 获取 akka Bytestring 的惯用方法是什么?
使用 map
Source
转换字节串
FileIO.fromPath(Paths.get("some file path")).map { byteString =>
doSomething()
}
我认为最简单的方法是使用 runFold
:
FileIO.fromPath(Paths.get("some file path"))
.runFold(ByteString.empty)(_ ++ _)
这个returns一个Future[ByteString]
.
也就是说,很可能只是用 Files.readAllBytes
加载整个文件,然后将结果 Array[Byte]
转换为 ByteString
会更有效率:
ByteString(Files.readAllBytes(Paths.get("some file path")))