FileIO.fromPath

Emits the contents of a file from the given path.

File IO Sinks and Sources

Signature

FileIO.fromPathFileIO.fromPath

Description

Emits the contents of a file from the given path, as ByteStrings, materializes into a Future CompletionStage which will be completed with an IOResult on end-of-file, cancellation, or failure. If the source opened the file, it attempts to close it before completing the materialized result.

Emitted elements have the configured chunkSize in bytes, except that the final element may be smaller. A short read that returns some bytes does not by itself end the stream.

Example

Scala
sourceimport org.apache.pekko.stream.scaladsl._
val file = Paths.get("example.csv")

val foreach: Future[IOResult] = FileIO.fromPath(file).to(Sink.ignore).run()
Java
sourcefinal Path file = Paths.get("example.csv");
  Sink<ByteString, CompletionStage<Done>> printlnSink =
      Sink.<ByteString>foreach(chunk -> System.out.println(chunk.utf8String()));

  CompletionStage<IOResult> ioResult = FileIO.fromPath(file).to(printlnSink).run(system);