FileIO.toPath

Create a sink which will write incoming ByteString s to a given file path.

File IO Sinks and Sources

Signature

def toPath(f: Path, options: Set[OpenOption] = Set(WRITE, TRUNCATE_EXISTING, CREATE)): Sink[ByteString, Future[IOResult]]
def toPath(f: Path, options: Set[OpenOption], startPosition: Long): Sink[ByteString, Future[IOResult]]

Description

Creates a Sink which writes incoming ByteString elements to the given file path. Overwrites existing files by truncating their contents as default. Materializes a Future CompletionStage of IOResult that will be completed with the size of the file (in bytes) at the streams completion, and a possible exception if IO operation was not completed successfully.

Example

Scala
val file = Paths.get("greeting.txt")
val text = Source.single("Hello Akka Stream!")
val result: Future[IOResult] = text
  .map(t ⇒ ByteString(t))
  .runWith(FileIO.toPath(file))
Java
final Path file = Paths.get("greeting.txt");
Sink<ByteString, CompletionStage<IOResult>> fileSink = FileIO.toPath(file);
Source<String, NotUsed> textSource = Source.single("Hello Akka Stream!");

CompletionStage<IOResult> ioResult = textSource
  .map(ByteString::fromString)
  .runWith(fileSink, mat);
Found an error in this documentation? The source code for this page can be found here. Please feel free to edit and contribute a pull request.